Angular 4 生成组件错误

3

我是angular 4的初学者。我正在尝试使用angular CLI创建新组件。

command : ng generate component myFolder/newComponent

But I am getting below error: 

Error: tree.branch is not a function
tree.branch is not a function.

你能分享一些代码吗,或者在仓库中分享它。 - Renil Babu
抱歉,无法完成翻译。 - Swapnil Yeole
5个回答

2

这可能与您的angular-cli安装/版本有关。尝试卸载/重新安装它:

通常情况下,重新安装即可解决问题。

npm uninstall -g @angular/cli
npm install -g @angular/cli

重新安装本地项目:

npm uninstall --save @angular/cli
npm install --save @angular/cli

不行,出错了。我得到了这个错误 Failed to compile. ./node_modules/process/browser.js 模块构建失败:错误:ENOENT:没有这样的文件或目录,打开'C:\ Users \ user \ angular2 \ my_proj \ proj1 \ node_modules \ process \ browser.js' @ ./node_modules/zone.js/dist/zone.js 1:0-37 @ ./src/polyfills.ts @ multi ./src/polyfills.ts - Swapnil Yeole
你在尝试重新安装后遇到了这个错误吗?查看这篇文章,它似乎是你的错误:https://stackoverflow.com/questions/46695065/angular-4-tree-branch-is-not-a-function-when-trying-to-start-a-new-project - Boulboulouboule
我不认为这会是一个解决方案,而且我的错误出现在不同的情况下。 - Swapnil Yeole
不要从我这里返回任何东西。你的解决方案可能对其他程序员有用。我正在手动创建组件。 - Swapnil Yeole
尝试手动删除 node_modules 文件夹并重新安装依赖项。 - Boulboulouboule
显示剩余2条评论

1

使用>cd myFolder命令转到myFolder文件夹。

然后使用>ng generate component newComponent命令。

也可以使用>ng g c newComponent命令。


抱歉耽搁了。它现在按照我想要的方式运行。 - Swapnil Yeole

0

你确定 myFolder 是你项目中的一个模块吗?如果它只是一个普通的文件夹,我建议手动使用 cd 进入该文件夹,然后执行以下操作:

ng g c your-component-name

当你输入 ng g c folder-name/your-component-name 时,Angular 期望 folder-name 实际上是一个包含 .module.ts 文件的模块。


0

现在我们可以做一件事情,使用 cd myFolder 命令进入文件夹。

C:\project\myFolder> ng g c newComponent --flat

0
删除 node_modules 文件夹,然后进入您想要生成新组件的路径(myFolder),并运行以下命令。
ng g component my-new-component

你运行这个命令时收到了什么错误信息?尝试删除节点模块文件夹并重新运行此命令。 - Prithivi Raj
我收到了相同的错误信息:“错误:tree.branch不是一个函数 tree.branch不是一个函数。” - Swapnil Yeole
请参考此处 https://github.com/angular/angular-cli/issues/8053,最新的ionic-cli中出现了这个问题。解决方法在相同的链接中提供。 - Prithivi Raj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接