我尝试使用“ng destroy component foo”,它告诉我“Angular-CLI不支持销毁命令”

如何使用Angular CLI正确地删除组件?


当前回答

我写了一个bash脚本,它应该自动执行下面Yakov Fain所写的过程。它可以像。/removeComponent这样调用

#!/bin/bash
if [ "$#" -ne 1 ]; then
    echo "Input a component to delete"
    exit 1
fi

# finds folder with component name and deletes
find . -type d -name $1 | xargs rm -rf

# removes lines referencing the component from app.module.ts
grep -v $1 app.module.ts > temp
mv temp app.module.ts

componentName=$1
componentName+="Component"

grep -v -i $componentName app.module.ts > temp
mv temp app.module.ts

其他回答

因为angular CLI还不支持它

所以这里是可能的方法,在此之前,请观察当你使用CLI创建一个组件/服务时会发生什么(ex. ng g c demoComponent)。

它创建了一个名为demoComponent的单独文件夹。 它生成HTML,CSS,ts和一个专门用于demoComponent的规范文件。 此外,它还在app.module.ts文件中添加依赖项,将该组件添加到项目中。

所以按相反的顺序来做

从app.module.ts中移除依赖项 删除该组件文件夹。

在消除依赖时,您必须做两件事。

从app.module.ts文件中删除导入行引用。 从app.module.ts中的@NgModule声明数组中删除组件声明。

从app.module.ts:

删除特定组件的导入行; 删除它在@NgModule中的声明;

然后删除想要删除的组件的文件夹及其包含的文件(.component.ts, .component.html, .component.css和.component.spec.ts)。

完成了。

-

我看到有人说要把它从main。ts中删除。你不应该首先从那里导入它,因为它已经导入了AppModule,而AppModule是导入你创建的所有组件的那个。

目前,Angular CLI不支持删除组件的选项,你需要手动执行。

从app.module中删除每个组件的导入引用 删除组件文件夹。 你还需要从app.module.ts文件中的@NgModule声明数组中删除组件声明

我不确定这是否是最好的方法,但它对我来说很有效。

首先,我删除了组件文件夹。 然后,我清除了app.module。与我想删除的组件相关的导入和声明的app.component.ts、app.component.ts和app.component.html。 类似地,我清除了main.ts。

我只是保存并刷新了应用程序,它工作了。

我写了一个bash脚本,它应该自动执行下面Yakov Fain所写的过程。它可以像。/removeComponent这样调用

#!/bin/bash
if [ "$#" -ne 1 ]; then
    echo "Input a component to delete"
    exit 1
fi

# finds folder with component name and deletes
find . -type d -name $1 | xargs rm -rf

# removes lines referencing the component from app.module.ts
grep -v $1 app.module.ts > temp
mv temp app.module.ts

componentName=$1
componentName+="Component"

grep -v -i $componentName app.module.ts > temp
mv temp app.module.ts