当我尝试在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

其他回答

当app文件夹下有多个模块时,使用以下命令生成组件将失败:

ng generate component New-Component-Name

原因是angular CLI检测到多个模块,不知道该在哪个模块中添加组件。因此,您需要显式地提到将添加哪个模块组件:

ng generate component New-Component-Name --module=ModuleName

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

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

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

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

试试这个:它对我很有效

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

只是对Zisha的答案做了一个小小的更新。

在我的项目中,所有的模块都放在一个名为“modules”的文件夹中,所有的组件都放在“src/app”下的“components”文件夹中。

所以要在特定的路径下创建一个组件,我使用以下语法:

components_path/component_name——module modules_path/module_name

例子:

Ng g c组件/登录——模块模块/app

当app或lib有多个嵌套模块时

myApp
  > src
    > app
      app.module.ts
      > routes
        > login
          > auth
          login.module.ts

角CLI

ng g component routes/login/auth/my-auth --module=routes/login/login.module.ts 

NRWL NX Angular CLI

ng g component routes/login/auth/my-auth --project=myApp --module=routes/login/login.module.ts

结果

myApp
  > src
    > app
      app.module.ts
      > routes
        > login
          > auth
            > my-auth
              my-auth.component.ts etc...
          login.module.ts