网站搜索

typescript - Angular-CLI。如何在当前模块中注册新模块?


我已经生成了一个模块:

 ng generate module myTestModule
installing module
  create src/app/my-test-module/my-test-module.module.ts

 ng generate module myTestModule2
installing module
  create src/app/my-test-module2/my-test-module2.module.ts

我有一个通常的文件描述主模块 app.module.ts。我该如何在 app.module.ts 中注册模块 my-test-module.module.ts,并在 my-test-module.module.ts 中注册 my-test-module2.module.ts?

一些帮助改进答案的更多细节

ng generate --help

module <name> <options...>
  aliases: m
  --spec (Boolean) Specifies if a spec file is generated.
  --flat (Boolean) Flag to indicate if a dir is created.
  --routing (Boolean) (Default: false) Specifies if a routing module file should be generated.
  --app (String) Specifies app name to use.
    aliases: -a <value>

这里没有像维基上声明的那样的 -m 或 --module

我的 Angular CLI:

ng -v

@angular/cli: 1.0.1
node: 7.4.0
@angular/common: 4.1.3
@angular/compiler: 4.1.3
@angular/core: 4.1.3
@angular/forms: 4.1.3
@angular/http: 4.1.3
@angular/platform-browser: 4.1.3
@angular/platform-browser-dynamic: 4.1.3
@angular/router: 4.1.3
@angular/cli: 1.0.1
@angular/compiler-cli: 4.1.3

一些帮助改进答案的更多细节 v2 我已将 angular-cli 更新到 v1.2.1。我可以使用别名 -m

module <name> <options...>
  aliases: m
  --spec (Boolean) Specifies if a spec file is generated.
  --flat (Boolean) Flag to indicate if a dir is created.
  --routing (Boolean) (Default: false) Specifies if a routing module file should be generated.
  --app (String) Specifies app name to use.
    aliases: -a <value>
  --module (String) Specifies where the module should be imported.
    aliases: -m <value>

所以我尝试

ng g m MyTestModuleModule34 -m AppModule

installing module
Specified module does not exist

但指定的模块存在于 src/app/app-module.ts

答案#1

当你生成模块时,你可以使用 -m 标志并指定你想要注册模块的名称。然后 CLI 会为你注册该模块。

答案#2

我不确定为什么 -m 不起作用,但你可以手动导入它们,只需要两秒钟。

app.module.ts

@NgModule({
  imports: [
    MyTestModule
  ]
})

my-test-module.module.ts

@NgModule({
  imports: [
    MyTestModule2 // This should be named MyTest2Module for naming consistancy throughout your app. i.e. all module names should end with the word module. See the Angular Style Guide for more info.
  ]
})

顺便提一下,没有理由在任何名称中包含两次 Module。相反,只需使用 my-test.module.ts 和 my-test-two.module.ts