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

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

例如:ng g module newModule

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

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


当前回答


在特定的模块中创建模块、服务和组件

Basic:

    ng g module chat     
    ng g service chat/chat -m chat
    ng g component chat/chat-dialog -m chat

    In chat.module.ts:
        exports: [ChatDialogComponent],
        providers: [ChatService]

    In app.module.ts:

        imports: [
            BrowserModule,
            ChatModule
        ]

    Now in app.component.html:
        <chat-dialog></chat-dialog>


LAZY LOADING:
    ng g module pages --module app.module --route pages

        CREATE src/app/pages/pages-routing.module.ts (340 bytes)
        CREATE src/app/pages/pages.module.ts (342 bytes)
        CREATE src/app/pages/pages.component.css (0 bytes)
        CREATE src/app/pages/pages.component.html (20 bytes)
        CREATE src/app/pages/pages.component.spec.ts (621 bytes)
        CREATE src/app/pages/pages.component.ts (271 bytes)
        UPDATE src/app/app-routing.module.ts (8611 bytes)       

    ng g module pages/forms --module pages/pages.module --route forms

        CREATE src/app/forms/forms-routing.module.ts (340 bytes)
        CREATE src/app/forms/forms.module.ts (342 bytes)
        CREATE src/app/forms/forms.component.css (0 bytes)
        CREATE src/app/forms/forms.component.html (20 bytes)
        CREATE src/app/forms/forms.component.spec.ts (621 bytes)
        CREATE src/app/forms/forms.component.ts (271 bytes)
        UPDATE src/app/pages/pages-routing.module.ts (437 bytes)

其他回答

使用这个简单的命令:

ng g c users/userlist

users:您的模块名。

userlist:你的组件名。

这对我来说很管用:

1 -->  ng g module new-module

2 -->  ng g c new-module/component-test --module=new-module/new-module.module.ts

如果你想生成一个没有目录的组件,使用——flat标志。

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

要将组件创建为模块的一部分,您应该

生成一个模块, cd newModule将目录更改为newModule文件夹 创建一个组件作为该模块的子组件。

更新:Angular 9

现在,生成组件时在哪个文件夹中都不重要了。

ng module NewModule生成一个模块。 new-module/new-component创建NewComponent。

注意:当Angular CLI看到new-module/new-component时,它会理解并转换case以匹配new-module -> NewModule和new-component -> NewComponent。刚开始可能会让人困惑,所以简单的方法是将#2中的名称与模块和组件的文件夹名称匹配。

我使用这个特殊的命令在模块内生成组件。

ng g c <module-directory-name>/<component-name>

该命令将在模块本地生成组件。 或者您可以先输入更改目录。

cd <module-directory-name>

然后创建组件。

ng g c <component-name>

注意:<>中包含的代码表示用户特定的名称。