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

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

例如:ng g module newModule

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

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


当前回答

使用这个简单的命令:

ng g c users/userlist

users:您的模块名。

userlist:你的组件名。

其他回答

首先,你必须使用以下命令创建一个新模块:

ng g module newModule

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

cd command  
cd newModule

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

ng g component newComponent

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

ng g component nameComponent --module=app.module.ts

首先,通过执行生成一个模块。

ng g m modules/media

这将在modules文件夹中生成一个名为media的模块。

其次,生成添加到该模块的组件

ng g c modules/media/picPick --module=modules/media/media.module.ts

ng g c modules/media/picPick命令的第一部分将在modules/media文件夹中生成一个名为picPick的组件文件夹,其中包含我们的新媒体模块。

第二部分将在media模块中导入新的picPick组件,并将其附加到该模块的declarations数组中。

1.-像往常一样创建你的特性模块。

ng generate module dirlevel1/module-name

2.-你可以在——module中指定项目的根路径(只有在——module中,(/)根指向你的项目根,而不是系统根!!)

ng generate component dirlevel1/component-name --module /src/app/dirlevel1/module-name.module.ts

真实的例子:

ng generate module stripe/payment-methods-list
ng generate component stripe/payment-methods-list --module=/src/app/stripe/payment-methods-list/payment-methods-list.module.ts 

输出:

CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.scss (0 bytes)
CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.html (39 bytes)
CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.spec.ts (768 bytes)
CREATE src/app/stripe/payment-methods-list/payment-methods-list.component.ts (322 bytes)
UPDATE src/app/stripe/payment-methods-list/payment-methods-list.module.ts (311 bytes)
[OK] Generated component!

用Angular CLI测试:9.1.4

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

@NgModule({
    declarations: [

    FileManagerDetailsComponent <---this is ur page component