【问题标题】:ng generate component in subdirectoryng 在子目录中生成组件
【发布时间】:2018-01-04 20:11:40
【问题描述】:

我有以下目录结构

我想创建一个新页面,比如说一个关于页面。 我想把它放在 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 在子目录中生成组件?

【问题讨论】:

  • 显然,我可以在生成它们后手动移动它们,但如果能够跳过该手动步骤会很好

标签: angular-cli angular5 ng-generate


【解决方案1】:

因为发现了 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

【讨论】:

  • 您不需要在模块名称后包含.module
【解决方案2】:

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

cd newModule to change directory into the newModule folder

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

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

【讨论】:

    【解决方案3】:

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

    $> 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 "
    

    【讨论】:

      【解决方案4】:

      指定您的模块

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

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

      ng generate component pages/about --module=app
      

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

      ng generate component pages/about --module=shared
      

      使用简写,这些命令可以缩写为:

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

      你的文件结构看起来也很糟糕。我建议将pages 目录中的所有文件上移一级到app 目录。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-30
        • 2015-05-27
        • 2012-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多