我开始使用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 m moduleName --routing
这将创建一个moduleName文件夹,然后导航到module文件夹
cd moduleName
然后生成组件:
ng g c componentName --module=moduleName.module.ts --flat
使用——flat表示不在模块文件夹中创建子文件夹
其他回答
首先,通过执行生成一个模块。
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数组中。
首先,你必须使用以下命令创建一个新模块:
ng g module newModule
然后,你必须使用以下命令进入该目录:
cd command
cd newModule
现在你可以继续使用下面的命令:
ng g component newComponent
这个组件会在那个模块中创建。
ng g c模块
然后在共享模块中创建头组件
ng g c shared/header --module=shared
我用特定的根文件夹创建了基于组件的子模块
下面是我指定的cli命令,请检查
ng g c Repair/RepairHome -m Repair/repair.module
Repair是子模块的根文件夹
-m是——module C代表复合 G代表生成
一种常见的模式是创建带有路由、惰性加载模块和组件的特性。
路线:myapp.com/feature
app-routing.module.ts
{ path: 'feature', loadChildren: () => import('./my-feature/my-feature.module').then(m => m.MyFeatureModule) },
文件结构:
app
└───my-feature
│ │ my-feature-routing.module.ts
│ │ my-feature.component.html
│ │ my-feature.component.css
│ │ my-feature.component.spec.ts
│ │ my-feature.component.ts
│ │ my-feature.module.ts
这一切都可以在cli中完成:
ng generate module my-feature --module app.module --route feature
或更短
ng g m my-feature --module app.module --route feature
或者如果你省略了名字,cli会提示你输入。在需要创建多个特性时非常有用
ng g m --module app.module --route feature