繁体   English   中英

如何在angular-cli中将组件生成路径更改为当前路径?

[英]How can I change the path of component generation to current path in angular-cli?

我正在学习 angular 2 课程,讲师导航到他想要生成组件的特定路径,然后运行以下命令:

ng generate component component-name

在他运行它之后,该工具会在当前目录中生成文件,但是当我尝试相同时,该工具最终会在应用程序根文件夹中生成文件。

换句话说,如果我这样做:

app\my-component> ng generate component component-name

我希望命令在这里生成文件:

app\my-component\component-name\
  component-name.component.html
  component-name.component.ts
  ...

而是在这里做:

app\component-name\
  component-name.component.html
  component-name.component.ts
  ...

我怎样才能告诉 ng-cli 使用当前路径?

ng g c 'path/to/component/componentName'

使用 angular cli 时,您永远不必离开根目录。 如果你想让你所有的组件都进入一个名为 my-component 的子目录,你只需执行ng gc my-component/componentName

请注意,g 和 c 分别是generatecomponent的有效简写形式。

假设你有结构src/app并且你想创建一个位于src/app/components/的组件header ,你可以运行ng gc components/header ,它将在src/app/components/header文件夹中生成文件,比如src/app/components/header/header.component.ts src/app/components/header/header.component.html

好吧,在谷歌搜索和阅读其他答案之后,在这里进行一些解释会很有帮助。 在你的项目中生成一个组件:

cd  path/your-project

在控制台(节点提示符或您的 IDE 控制台)中键入:

ng generate component components/your-new-component-name

如果你想缩写:

ng g c components/your-new-component-name

您可以创建其他角度组件,例如:ng g 指令 ng g 模块...

来源和更多信息: Angular 官方文档

你应该避免: 'route-to-your-project':这会在你执行时连接路由: ng gc 'route-to-your-project' 并且控制台会显示错误,因为找不到正确的生成位置.

例子:

 ng g c components/alerts/test3

CREATE src/app/components/alerts/test3/test3.component.html (20 bytes)
CREATE src/app/components/alerts/test3/test3.component.spec.ts (621 bytes)
CREATE src/app/components/alerts/test3/test3.component.ts (266 bytes)
CREATE src/app/components/alerts/test3/test3.component.scss (0 bytes)
UPDATE src/app/components/alerts/alerts.module.ts (5046 bytes)

希望对您有所帮助

在学习 Max Schwartzmuller 的 Udemy 课程时,我遇到了类似的问题。 我试图在与应用程序平行的子文件夹中生成组件。 cli 将直接在应用程序下创建任何以这种方式调用的东西。

c:\ng2\aufbs\src\mycomponents>ng g c my-test
installing component
  create src\app\my-test\my-test.component.css
  create src\app\my-test\my-test.component.html
  create src\app\my-test\my-test.component.spec.ts
  create src\app\my-test\my-test.component.ts

在 app 下移动 /mycomponents 一切都按预期工作。

从 app 文件夹设置路径,所以你不需要使用ng gc 'C:\someFolder\your-project\src\app\some-folder\other-folder\your-component' 只需使用ng gc '.\pages\shered\reset-password'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM