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

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

例如:ng g module newModule

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

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


当前回答

今天我在搭建一个Angular 9应用程序时遇到了这个问题。我得到了“模块不存在错误”每当我添加.module。Ts或.module到模块名。cli只需要模块名,不需要扩展名。假设我有一个模块名:brands.module。ts,我使用的命令是

ng g c path/to/my/components/brands-component -m brands --dry-run

一旦确认文件结构正确,就删除——dry-run。

其他回答

这对我来说很管用:

1 -->  ng g module new-module

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

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

一种常见的模式是创建带有路由、惰性加载模块和组件的特性。

路线:myapp.com/feature

app-routing.module.ts

{ path: 'feature', loadChildren: () => import('./my-feature/my-feature.module').then(m => m.MyFeatureModule) },

文件结构:

app   
└───my-feature
│   │   my-feature-routing.module.ts
│   │   my-feature.component.html
│   │   my-feature.component.css
│   │   my-feature.component.spec.ts
│   │   my-feature.component.ts
│   │   my-feature.module.ts

这一切都可以在cli中完成:

ng generate module my-feature --module app.module --route feature

或更短

ng g m my-feature --module app.module --route feature

或者如果你省略了名字,cli会提示你输入。在需要创建多个特性时非常有用

ng g m --module app.module --route feature

首先,你必须使用以下命令创建一个新模块:

ng g module newModule

然后,你必须使用以下命令进入该目录:

cd command  
cd newModule

现在你可以继续使用下面的命令:

ng g component newComponent

这个组件会在那个模块中创建。


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

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路径/to/folder/component—路由路径/to/folder/component

这将创建一个包含6个文件的文件夹。

CREATE path/to/folder/component/component-routing.module.ts (253 bytes)
CREATE path/to/folder/component/component.module.ts (297 bytes)
CREATE path/to/folder/component/component.component.html (26 bytes)
CREATE path/to/folder/component/component.component.spec.ts (655 bytes)
CREATE path/to/folder/component/component.component.ts (295 bytes)
CREATE path/to/folder/component/component.component.scss (0 bytes)
UPDATE path/to/folder/component/component.module.ts (379 bytes)

如果你不想要路由,你可以删除。