我开始使用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或我必须手动这样做?
当前回答
对于Angular v4及以上版本,只需使用:
ng g c componentName -m ModuleName
其他回答
我在应用程序中有多个模块的类似问题。可以为任何模块创建组件,因此在创建组件之前,必须指定特定模块的名称。
'ng generate component newCompName --module= specify name of module'
根据Angular文档,为特定模块创建组件的方法是:
ng g component <directory name>/<component name>
"目录名" = CLI生成特性模块的目录
例子:-
ng generate component customer-dashboard/CustomerDashboard
这将在customer-dashboard文件夹中为新组件生成一个文件夹,并使用CustomerDashboardComponent更新特性模块
我不知道这是不是把你们带到这里来的原因,但我想创建一个包含模块、路由和组件文件的文件夹。我希望将组件声明给我创建的模块。
为此,我发现这个命令非常有用。
Ng路径/to/folder/component—路由路径/to/folder/component
这将创建一个包含6个文件的文件夹。
CREATE path/to/folder/component/component-routing.module.ts (253 bytes)
CREATE path/to/folder/component/component.module.ts (297 bytes)
CREATE path/to/folder/component/component.component.html (26 bytes)
CREATE path/to/folder/component/component.component.spec.ts (655 bytes)
CREATE path/to/folder/component/component.component.ts (295 bytes)
CREATE path/to/folder/component/component.component.scss (0 bytes)
UPDATE path/to/folder/component/component.module.ts (379 bytes)
如果你不想要路由,你可以删除。
首先运行ng g module newModule . 然后执行ng g component newModule/newModule——flat命令
如果你在.angular-cli中声明了多个应用。Json(例如在处理特性模块的情况下)
"apps": [{
"name": "app-name",
"root": "lib",
"appRoot": ""
}, {...} ]
你可以:
ng g c my-comp -a app-name
-a代表——app (name)