Create component to specific module with Angular-CLI

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

    if you want to create along with your module try this

      ng generate m module_name --routing &&  ng generate c component_name
    
    0 讨论(0)
  • 2021-01-29 20:20
    ng g component nameComponent --module=app.module.ts
    
    0 讨论(0)
  • 2021-01-29 20:20

    1.- Create your feature module as usual.

    ng generate module dirlevel1/module-name
    

    2.- You can specify the ROOT PATH of your project in --module ( only in --module, (/) root points to your PROJECT ROOT and IS NOT THE SYSTEM ROOT!!!)

    ng generate component dirlevel1/component-name --module /src/app/dirlevel1/module-name.module.ts
    

    Real Example:

    ng generate module stripe/payment-methods-list
    
    ng generate component stripe/payment-methods-list --module=/src/app/stripe/payment-methods-list/payment-methods-list.module.ts 
    

    Output:

    CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.scss (0 bytes)
    CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.html (39 bytes)
    CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.spec.ts (768 bytes)
    CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.ts (322 bytes)
    UPDATE src/app/stripe/payment-methods-list/payment-methods-list.module.ts (311 bytes)
    [OK] Generated component!
    

    Tested with Angular CLI: 9.1.4

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

    If you have multiple apps declared in .angular-cli.json ( e.g. in case working on feature module)

    "apps": [{
        "name": "app-name",
        "root": "lib",
        "appRoot": ""
    }, {...} ]
    

    You can :

    ng g c my-comp -a app-name
    

    -a stands for --app (name)

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

    this is what worked for me :

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

    If you want to generate a component without its directory use --flat flag.

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

    I use this particular command for generating components inside a module.

    ng g c <module-directory-name>/<component-name>
    

    This command will generate component local to the module. or You can change directory first by typing.

    cd <module-directory-name>
    

    and then create component.

    ng g c <component-name>
    

    Note: code enclosed in <> represent user specific names.

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