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

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

例如:ng g module newModule

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

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


当前回答

如果你在.angular-cli中声明了多个应用。Json(例如在处理特性模块的情况下)

"apps": [{
    "name": "app-name",
    "root": "lib",
    "appRoot": ""
}, {...} ]

你可以:

ng g c my-comp -a app-name

-a代表——app (name)

其他回答

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

@NgModule({
    declarations: [

    FileManagerDetailsComponent <---this is ur page component

第一个生成模块:

ng g m moduleName --routing

这将创建一个moduleName文件夹,然后导航到module文件夹

cd moduleName

然后生成组件:

ng g c componentName --module=moduleName.module.ts --flat

使用——flat表示不在模块文件夹中创建子文件夹

我在应用程序中有多个模块的类似问题。可以为任何模块创建组件,因此在创建组件之前,必须指定特定模块的名称。

'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 g module newModule

然后,你必须使用以下命令进入该目录:

cd command  
cd newModule

现在你可以继续使用下面的命令:

ng g component newComponent

这个组件会在那个模块中创建。