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?
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.
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.
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.
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.
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.
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.
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
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
With above steps after renaming the variables and files you can achieve the angular component renaming.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With