我开始使用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 component nameComponent --module=app.module.ts
其他回答
我用特定的根文件夹创建了基于组件的子模块
下面是我指定的cli命令,请检查
ng g c Repair/RepairHome -m Repair/repair.module
Repair是子模块的根文件夹
-m是——module C代表复合 G代表生成
使用这个简单的命令:
ng g c users/userlist
users:您的模块名。
userlist:你的组件名。
我不知道这是不是把你们带到这里来的原因,但我想创建一个包含模块、路由和组件文件的文件夹。我希望将组件声明给我创建的模块。
为此,我发现这个命令非常有用。
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)
如果你不想要路由,你可以删除。
如果你在.angular-cli中声明了多个应用。Json(例如在处理特性模块的情况下)
"apps": [{
"name": "app-name",
"root": "lib",
"appRoot": ""
}, {...} ]
你可以:
ng g c my-comp -a app-name
-a代表——app (name)
这对我来说很管用:
1 --> ng g module new-module
2 --> ng g c new-module/component-test --module=new-module/new-module.module.ts
如果你想生成一个没有目录的组件,使用——flat标志。