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

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

例如:ng g module newModule

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

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


当前回答

我在angular CLI版本8.3中使用过以下命令,它通过生成在特定模块中声明的特定组件来工作。

移动到我们需要生成组件的特定文件夹 cd <组件路径> 调用下面的generate命令 Ng c <component-name>——module=<module-name>

例子

 `ng g c login --module= app`

其他回答

使用这个简单的命令:

ng g c users/userlist

users:您的模块名。

userlist:你的组件名。


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

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 component "path to your component"/NEW_COMPONENT_NAME -m "MODULE_NAME"

例子:

ng g component common/signup/payment-processing/OnlinePayment -m pre-login.module

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

'ng generate component newCompName --module= specify name of module'
ng g c componentName --module=path-to-your-module-from-src-folder

例子:

ng g c testComponent --module=/src/app/home/test-component/test-component.module