Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a component in Angular CLI?

Is there any shortcut to rename a component with the Angular CLI other than manually editing all the component files such as folder name, .css, .ts, spec.ts and app.module.ts?

like image 425
Thomas Easo Avatar asked Oct 10 '22 00:10

Thomas Easo


People also ask

Can I rename component in angular?

Currently Angular CLI doesn't support the feature of renaming or refactoring code. You can achieve such functionality with the help of some IDE. Intellij, Eclipse, VSCode etc.. has default support the refactoring.

How do you rename a component in VSCode?

With the cursor at the Component class, open Command Palette in VSCode with cmd+shift+p . Select “Rename Symbol” command and change the class name. VSCode will change the class name, and update all the references to this class in other files. This is safer than using global search and replace, in my opinion.

Can We have a rename command in angular CLI?

I am a frequent user of angular cli and I think this would be a nice-to-have command as some time we just create angular components and need to rename them. As this rename command is not there, deleting the created angular component, directive, etc and again running the command to create the component is really a pain.

How do I create a new component in angular CLI?

Creating a component using the Angular CLI link To create a component using the Angular CLI: From a terminal window, navigate to the directory containing your application. Run the ng generate component <component-name> command, where <component-name> is the name of your new component.

Is there a way to rename a component created using component create?

There is no command which will change the name of all files generated using the component create command. So created ts, html, css/scss, .spec.ts files need to be manually renamed/edited. I am a frequent user of angular cli and I think this would be a nice-to-have command as some time we just create angular components and need to rename them.

Is there a RM remove command for angular components?

It will be nice-to-have rm remove command as some time we just create angular components and need to rename them. As this rename command is not there, deleting the created angular component, directive, etc and again running the command to create the component is a pain. Sorry, something went wrong.


3 Answers

No!

There is no command which will change the name of all files generated using the component create command. So created ts, html, css/scss, .spec.ts files need to be manually renamed/edited.

I am a frequent user of angular cli and I think this would be a nice-to-have command as some time we just create angular components and need to rename them. As this rename command is not there, deleting the created angular component, directive, etc and again running the command to create the component is really a pain.

Here is the discussion link to add this feature rename angular component

like image 275
Aniruddha Das Avatar answered Oct 18 '22 23:10

Aniruddha Das


Currently Angular CLI doesn't support the feature of renaming or refactoring code.

You can achieve such functionality with the help of some IDE.

Intellij, Eclipse, VSCode etc.. has default support the refactoring.

Nowadays VSCode is showing some uptrend,personally I'm a fan of this

Refactoring with VSCode

Determinig reference : - VS Code help you find all references of a variable by selecting variable and pressing shortcut SHIFT + F12. This works incredibly well with Type Script.

Renaming all instances of reference :- After finding all the references you can press F2 will open a popup and you can change the value and click enter this will update all the instances of reference.

Renaming files and imports You can rename a file and its import references with a plugin. More details can be found here

enter image description here

With above steps after renaming the variables and files you can achieve the angular component renaming.

like image 68
SAMUEL Avatar answered Oct 18 '22 21:10

SAMUEL


Yes!

Now there is.

There is an Extension for VS Code to do all of these steps for you. It's called Rename Angular Component.

I have hopes of porting it to WebStorm and a Schematic later.

Previously, it took at least three semi-automated steps in VS Code as a minimum. This Extension does all of them.

I realise this is promoting my own solution. But it is free, open source, and the complete answer. If it doesn't do everything you're looking for, you can post issues on the repo, and it will be improved.

like image 42
tomwhite007 Avatar answered Oct 18 '22 22:10

tomwhite007