我开始使用angular-cli,我已经阅读了很多关于我想做什么的答案…没有成功,所以我来到这里。

是否有一种方法可以为新模块创建组件?

例如:ng g module newModule

如何将该组件添加到newModule中??

因为angular-cli的默认行为是把所有新组件放到app.module中。我想选择我的组件将在哪里,这样我就可以创建单独的模块,而不会有我所有的组件在app.module。这是可能的使用angular-cli或我必须手动这样做?


当前回答

你可以使用ng generate Module <模块名>来生成一个Module。 然后使用此命令生成与该模块相关的组件,ng generate component <组件名称>——module=<模块名称>

其他回答

如果你已经创建了你的组件而没有指向一个模块,你可以通过将它添加到声明中来直接将它添加到你的应用模块中

@NgModule({
    declarations: [

    FileManagerDetailsComponent <---this is ur page component

如果需要同时创建组件和模块,可以使用命令

ng g m components/my-control && ng g c $_

这将在同一个目录中创建一个模块和组件,默认情况下,组件将被添加到closed模块中

如果你在.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标志。

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