我开始使用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 component "path to your component"/NEW_COMPONENT_NAME -m "MODULE_NAME"
例子:
ng g component common/signup/payment-processing/OnlinePayment -m pre-login.module
如果你在.angular-cli中声明了多个应用。Json(例如在处理特性模块的情况下)
"apps": [{
"name": "app-name",
"root": "lib",
"appRoot": ""
}, {...} ]
你可以:
ng g c my-comp -a app-name
-a代表——app (name)
Angular 13+ cli
我在模块文件夹下有多个模块
ng g c components/shared/newlayout -m modules/Shared
要将组件创建为模块的一部分,您应该
生成一个模块, cd newModule将目录更改为newModule文件夹 创建一个组件作为该模块的子组件。
更新:Angular 9
现在,生成组件时在哪个文件夹中都不重要了。
ng module NewModule生成一个模块。 new-module/new-component创建NewComponent。
注意:当Angular CLI看到new-module/new-component时,它会理解并转换case以匹配new-module -> NewModule和new-component -> NewComponent。刚开始可能会让人困惑,所以简单的方法是将#2中的名称与模块和组件的文件夹名称匹配。
你可以使用ng generate Module <模块名>来生成一个Module。 然后使用此命令生成与该模块相关的组件,ng generate component <组件名称>——module=<模块名称>