我开始使用angular-cli,我已经阅读了很多关于我想做什么的答案…没有成功,所以我来到这里。
是否有一种方法可以为新模块创建组件?
例如:ng g module newModule
如何将该组件添加到newModule中??
因为angular-cli的默认行为是把所有新组件放到app.module中。我想选择我的组件将在哪里,这样我就可以创建单独的模块,而不会有我所有的组件在app.module。这是可能的使用angular-cli或我必须手动这样做?
我开始使用angular-cli,我已经阅读了很多关于我想做什么的答案…没有成功,所以我来到这里。
是否有一种方法可以为新模块创建组件?
例如:ng g module newModule
如何将该组件添加到newModule中??
因为angular-cli的默认行为是把所有新组件放到app.module中。我想选择我的组件将在哪里,这样我就可以创建单独的模块,而不会有我所有的组件在app.module。这是可能的使用angular-cli或我必须手动这样做?
当前回答
ng g c componentName --module=path-to-your-module-from-src-folder
例子:
ng g c testComponent --module=/src/app/home/test-component/test-component.module
其他回答
使用Angular CLI将一个组件添加到Angular 4应用中
要向应用中添加一个新的Angular 4组件,请使用命令ng g component componentName。执行这个命令后,Angular CLI会在src\app下添加一个文件夹component-name。同样,同样的引用也被添加到src\app\app.module中。Ts文件自动。
组件应该有一个@Component装饰器函数,后面跟着一个需要导出的类。@Component装饰器函数接受元数据。
使用Angular CLI将一个组件添加到Angular 4应用的特定文件夹中
使用命令ng g component folderName/componentName将一个新组件添加到指定的文件夹中
我在angular CLI版本8.3中使用过以下命令,它通过生成在特定模块中声明的特定组件来工作。
移动到我们需要生成组件的特定文件夹 cd <组件路径> 调用下面的generate命令 Ng c <component-name>——module=<module-name>
例子
`ng g c login --module= app`
如果需要同时创建组件和模块,可以使用命令
ng g m components/my-control && ng g c $_
这将在同一个目录中创建一个模块和组件,默认情况下,组件将被添加到closed模块中
ng g c componentName --module=path-to-your-module-from-src-folder
例子:
ng g c testComponent --module=/src/app/home/test-component/test-component.module
1.-像往常一样创建你的特性模块。
ng generate module dirlevel1/module-name
2.-你可以在——module中指定项目的根路径(只有在——module中,(/)根指向你的项目根,而不是系统根!!)
ng generate component dirlevel1/component-name --module /src/app/dirlevel1/module-name.module.ts
真实的例子:
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
输出:
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!
用Angular CLI测试:9.1.4