我开始使用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或我必须手动这样做?
当前回答
今天我在搭建一个Angular 9应用程序时遇到了这个问题。我得到了“模块不存在错误”每当我添加.module。Ts或.module到模块名。cli只需要模块名,不需要扩展名。假设我有一个模块名:brands.module。ts,我使用的命令是
ng g c path/to/my/components/brands-component -m brands --dry-run
一旦确认文件结构正确,就删除——dry-run。
其他回答
第一个生成模块:
ng g m moduleName --routing
这将创建一个moduleName文件夹,然后导航到module文件夹
cd moduleName
然后生成组件:
ng g c componentName --module=moduleName.module.ts --flat
使用——flat表示不在模块文件夹中创建子文件夹
我没有找到一个答案,说明如何使用cli在顶级模块文件夹中生成组件,并让组件自动添加模块的声明集合。
要创建模块,运行以下命令:
ng g module foo
在foo module文件夹中创建组件,并将其添加到foo.module中。Ts的声明集合运行如下:
ng g component foo/fooList --module=foo.module.ts
cli会像这样构建模块和组件:
——编辑angular cli的新版本。1.5.5不需要模块文件名,所以v1.5.5的命令应该是
ng g component foo/fooList --module=foo
我在angular CLI版本8.3中使用过以下命令,它通过生成在特定模块中声明的特定组件来工作。
移动到我们需要生成组件的特定文件夹 cd <组件路径> 调用下面的generate命令 Ng c <component-name>——module=<module-name>
例子
`ng g c login --module= app`
我不知道这是不是把你们带到这里来的原因,但我想创建一个包含模块、路由和组件文件的文件夹。我希望将组件声明给我创建的模块。
为此,我发现这个命令非常有用。
Ng路径/to/folder/component—路由路径/to/folder/component
这将创建一个包含6个文件的文件夹。
CREATE path/to/folder/component/component-routing.module.ts (253 bytes)
CREATE path/to/folder/component/component.module.ts (297 bytes)
CREATE path/to/folder/component/component.component.html (26 bytes)
CREATE path/to/folder/component/component.component.spec.ts (655 bytes)
CREATE path/to/folder/component/component.component.ts (295 bytes)
CREATE path/to/folder/component/component.component.scss (0 bytes)
UPDATE path/to/folder/component/component.module.ts (379 bytes)
如果你不想要路由,你可以删除。
我使用这个特殊的命令在模块内生成组件。
ng g c <module-directory-name>/<component-name>
该命令将在模块本地生成组件。 或者您可以先输入更改目录。
cd <module-directory-name>
然后创建组件。
ng g c <component-name>
注意:<>中包含的代码表示用户特定的名称。