Create component to specific module with Angular-CLI

后端 未结 26 1630
既然无缘
既然无缘 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:15

    Read the description of --route https://angular.io/cli/generate#module-command,

    To archive such, you must add the route of that component-module to somewhere and specify the route name.

       ng generate module component-name --module=any-parent-module --route=route-path
    
    0 讨论(0)
  • 2021-01-29 20:16
    1. First, you generate a module by executing.
    ng g m modules/media
    

    this will generate a module called media inside modules folder.

    1. Second, you generate a component added to this module
    ng g c modules/media/picPick --module=modules/media/media.module.ts
    

    the first part of the command ng g c modules/media/picPick will generate a component folder called picPick inside modules/media folder witch contain our new media module.

    the second part will make our new picPick component declared in media module by importing it in the module file and appending it to declarations array of this module.

    0 讨论(0)
  • 2021-01-29 20:16

    I ran into this issue today while scaffolding an Angular 9 application. I got the "module does not exist error" whenever I added the .module.ts or .module to the module name. The cli only needs the name of the module with no extension. Assuming I had a module name: brands.module.ts, the command I used was

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

    remove the --dry-run once you've confirmed the file structure is correct.

    0 讨论(0)
  • 2021-01-29 20:16
    1. You can generate a Module using ng generate module <module name>.
    2. Then use this command to generate a component related to that module, ng generate component <component name> --module=<module name>
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-29 20:18

    Make a module, service and component in particular module

    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)
    
    0 讨论(0)
提交回复
热议问题