ng 在子目录中生成组件

ng generate component in subdirectory

我有以下目录结构

我想创建一个新页面,比方说,一个关于页面。 我想把它放在 src/app/page/about/*

所以我尝试:

ng generate component pages/about

但是我得到这个错误:

Error: More than one module matches. Use skip-import option to skip importing the component into the closest module.
More than one module matches. Use skip-import option to skip importing the component into the closest module.

使用页面来存储我的单独页面是个好主意吗? 如何使用 angular-cli 在子目录中生成组件?

因为有 2 个模块(app.module.tsshared.module.ts)发现 CLI 不知道要在哪个模块中声明您的组件。要解决这个问题,您必须告诉 CLI 您要在哪个模块中声明您的组件希望使用 module 选项

声明您的组件
ng generate component pages/about --module=app.module
// or
ng generate component pages/about --module=shared.module

一般情况下,需要在特定路径下生成组件,或者不超过一个父模块,如上例。

$> ng g component path/component-name  
  <enter> 

如果是 Angular 应用程序,则不需要 src/app/path/component-name ->path。 如果您提到 src/app,则会出现此错误。

"Error: More than one module matches. Use skip-import "

要创建一个组件作为任何新模块的一部分,您还可以这样做:

cd newModule to change directory into the newModule folder

ng g c newComponent to create a component as a child of the module.

这对我很有效。我收到的错误与您收到的错误相同。

指定您的模块

您有两个模块,但尚未指定要将新组件添加到哪个模块。

如果你想将它添加到你的 app 模块中,你可以简单地使用:

ng generate component pages/about --module=app

同样要将其添加到您的 shared 模块,您可以使用:

ng generate component pages/about --module=shared

使用shorthand,这些命令可以缩短为:

ng g c pages/about --module=app
ng g c pages/about --module=shared

你的文件结构也不对。我建议将 pages 目录中的所有文件向上移动到 app 目录。