我正在使用Angular 4和Angular CLI,我可以用下面的命令创建一个新组件。
E:\HiddenWords>ng generate component plainsight
但我需要在plainsight内生成一个子组件。有办法用Angular CLI来实现吗?
我正在使用Angular 4和Angular CLI,我可以用下面的命令创建一个新组件。
E:\HiddenWords>ng generate component plainsight
但我需要在plainsight内生成一个子组件。有办法用Angular CLI来实现吗?
当前回答
上面的选项不适合我,因为与在终端中创建目录或文件不同,当CLI生成一个组件时,它默认将路径src/app添加到您输入的路径中。
如果我从主应用文件夹中生成组件像这样(错误的方式)
ng g c ./src/app/child/grandchild
生成的组件是这样的:
src/app/src/app/child/grandchild.component.ts
所以我只需要打字
ng g c child/grandchild
希望这对大家有所帮助
其他回答
假设你想在一个模块Box中创建一个组件Container,模块已经创建好了,并且存在于它自己的文件夹src/app/ Box中。
您可以这样做——当被要求提供组件名称时,提供组件的路径和组件名称。
ng g c -m box
? What name would you like to use for the component? box/Container
CREATE src/app/box/container/container.component.scss (0 bytes)
CREATE src/app/box/container/container.component.html (24 bytes)
CREATE src/app/box/container/container.component.spec.ts (647 bytes)
CREATE src/app/box/container/container.component.ts (288 bytes)
UPDATE src/app/box/box.module.ts (286 bytes)
在一个特定的文件夹中创建一个组件:
ng g c folder-name/component-name
使用Angular-CLI在特定(现有)模块的文件夹中创建一个组件:
ng g c folder-name/component-name --module=folder-name/moduleName.module.ts
Ng g c组件名称
用于指定自定义位置:ng g c specific-folder/component-name
在这里,component-name将在specific-folder中创建。
类似的方法可以用于生成其他组件,如指令、管道、服务、类、保护、接口、枚举、模块等。
上面的选项不适合我,因为与在终端中创建目录或文件不同,当CLI生成一个组件时,它默认将路径src/app添加到您输入的路径中。
如果我从主应用文件夹中生成组件像这样(错误的方式)
ng g c ./src/app/child/grandchild
生成的组件是这样的:
src/app/src/app/child/grandchild.component.ts
所以我只需要打字
ng g c child/grandchild
希望这对大家有所帮助
在命令提示符或项目终端中转到项目文件夹。
执行cmd: ng g c componentname命令