我开始使用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 -> Angular
g -> Generate
c -> Component
-m -> Module
然后你的命令会像这样:
ng g c user/userComponent -m user.module
其他回答
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
第一个生成模块:
ng g m moduleName --routing
这将创建一个moduleName文件夹,然后导航到module文件夹
cd moduleName
然后生成组件:
ng g c componentName --module=moduleName.module.ts --flat
使用——flat表示不在模块文件夹中创建子文件夹
根据Angular文档,为特定模块创建组件的方法是:
ng g component <directory name>/<component name>
"目录名" = CLI生成特性模块的目录
例子:-
ng generate component customer-dashboard/CustomerDashboard
这将在customer-dashboard文件夹中为新组件生成一个文件夹,并使用CustomerDashboardComponent更新特性模块
这对我来说很管用:
1 --> ng g module new-module
2 --> ng g c new-module/component-test --module=new-module/new-module.module.ts
如果你想生成一个没有目录的组件,使用——flat标志。
首先运行ng g module newModule . 然后执行ng g component newModule/newModule——flat命令