Create component to specific module with Angular-CLI

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

    Add a component to the Angular 4 app using Angular CLI

    To add a new Angular 4 component to the app, use command ng g component componentName. After execution of this command, Angular CLI adds a folder component-name under src\app. Also, the references of the same is added to src\app\app.module.ts file automatically.

    A component shall have a @Component decorator function followed by a class which needs to be exported. The @Component decorator function accepts meta data.

    Add a component to specific folder of the Angular 4 app using Angular CLI

    To add a new component to a specific folder use the command ng g component folderName/componentName

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

    According to Angular docs the way to create a component for specific module is,

    ng g component <directory name>/<component name>
    

    "directory name" = where the CLI generated the feature module

    Example :-

    ng generate component customer-dashboard/CustomerDashboard
    

    This generates a folder for the new component within the customer-dashboard folder and updates the feature module with the CustomerDashboardComponent

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

    You can try below command, which describes the,

    ng -> Angular 
    g  -> Generate
    c  -> Component
    -m -> Module 
    

    Then your command will be like:

    ng g c user/userComponent -m user.module
    
    0 讨论(0)
  • 2021-01-29 20:15

    To create a component as part of a module you should

    1. ng g module newModule to generate a module,
    2. cd newModule to change directory into the newModule folder
    3. ng g component newComponent to create a component as a child of the module.

    UPDATE: Angular 9

    Now it doesn't matter what folder you are in when generating the component.

    1. ng g module NewMoudle to generate a module.
    2. ng g component new-module/new-component to create NewComponent.

    Note: When the Angular CLI sees new-module/new-component, it understands and translates the case to match new-module -> NewModule and new-component -> NewComponent. It can get confusing in the beginning, so easy way is to match the names in #2 with the folder names for the module and component.

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

    Use this simple command:

    ng g c users/userlist
    

    users: Your module name.

    userlist: Your component name.

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

    I created component based child module with specific Root Folder

    That cli command below i specified,please check out

    ng g c Repair/RepairHome -m Repair/repair.module
    

    Repair is Root Folder of our child module

    -m is --module

    c for compount

    g for generate

    0 讨论(0)
提交回复
热议问题