Create component to specific module with Angular-CLI

后端 未结 26 1633
既然无缘
既然无缘 2021-01-29 19:46

I\'m starting to use angular-cli and I\'ve already read a lot to find an answer about what I want to do...no success, so I came here.

Is there a way to create a componen

26条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 20:18

    A common pattern is to create a feature with a route, a lazy loaded module, and a component.

    Route: myapp.com/feature

    app-routing.module.ts

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

    File structure:

    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
    

    This can all be done in the cli with:

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

    Or shorter

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

    Or if you leave out the name the cli will prompt you for it. Very useful when you need to create several features

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

提交回复
热议问题