我开始使用angular-cli,我已经阅读了很多关于我想做什么的答案…没有成功,所以我来到这里。

是否有一种方法可以为新模块创建组件?

例如:ng g module newModule

如何将该组件添加到newModule中??

因为angular-cli的默认行为是把所有新组件放到app.module中。我想选择我的组件将在哪里,这样我就可以创建单独的模块,而不会有我所有的组件在app.module。这是可能的使用angular-cli或我必须手动这样做?


当前回答

1.-像往常一样创建你的特性模块。

ng generate module dirlevel1/module-name

2.-你可以在——module中指定项目的根路径(只有在——module中,(/)根指向你的项目根,而不是系统根!!)

ng generate component dirlevel1/component-name --module /src/app/dirlevel1/module-name.module.ts

真实的例子:

ng generate module stripe/payment-methods-list
ng generate component stripe/payment-methods-list --module=/src/app/stripe/payment-methods-list/payment-methods-list.module.ts 

输出:

CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.scss (0 bytes)
CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.html (39 bytes)
CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.spec.ts (768 bytes)
CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.ts (322 bytes)
UPDATE src/app/stripe/payment-methods-list/payment-methods-list.module.ts (311 bytes)
[OK] Generated component!

用Angular CLI测试:9.1.4

其他回答

如果你已经创建了你的组件而没有指向一个模块,你可以通过将它添加到声明中来直接将它添加到你的应用模块中

@NgModule({
    declarations: [

    FileManagerDetailsComponent <---this is ur page component

Angular 13+ cli

我在模块文件夹下有多个模块

ng g c components/shared/newlayout -m modules/Shared

我在应用程序中有多个模块的类似问题。可以为任何模块创建组件,因此在创建组件之前,必须指定特定模块的名称。

'ng generate component newCompName --module= specify name of module'

你可以使用ng generate Module <模块名>来生成一个Module。 然后使用此命令生成与该模块相关的组件,ng generate component <组件名称>——module=<模块名称>

阅读描述——route https://angular.io/cli/generate#module-command,

为了存档,您必须将该组件模块的路由添加到某个位置,并指定路由名称。

   ng generate module component-name --module=any-parent-module --route=route-path