当我尝试在angular cli中创建一个组件时,它显示了这个错误。我怎样才能摆脱它呢?

错误:多个模块匹配。使用skip-import选项可以跳过将组件导入到最近的模块。

我使用的是angular cli版本:1.4.1


当前回答

我的项目是使用Visual Studio Community 2017创建的,它创建了3个独立的模块:app.browser。Module, app.server.module和app.shared.module

为了创建我的组件,我检查了上面的答案,发现我的模块是app.share .module。

所以,我跑:

ng g c componentName --module=app.shared.module

其他回答

对我来说,这个命令工作:

ng g c new-component --module app

作为一个黑客,下面的步骤,对我有用。

1)移动*.module。Ts文件从src/app到项目外的位置。

2)执行命令创建组件[ng g c component-name]

3)移回*.模块。Ts文件到src/app/

使用——module参数指定模块。 例如,如果主模块是app.module。Ts,运行这个:

ng g c new-component --module app

或者如果你在另一个目录

ng g c component-name --module ../

试试这个:它对我很有效

生成组件componentName——module=app.module

有两种方法可以解决这个问题。

1)跳过(在命令中使用——Skip -import)默认导入并创建组件,一旦组件创建完成,就可以手动将它导入到任何你想使用它的地方。

ng generate component my-component --skip-import

2)在你想导入模块的地方显式地提供模块名

ng generate  component  my-component --module=my-module.module