Home Angular7 How to include custom js file to all components in Angular 7?

How to include custom js file to all components in Angular 7?

by therichpost
5 comments
angular7

Hello to all, welcome to therichpost.com. In this post, I will tell you, How to include custom js file to all components in Angular 7?

Angular 7 getting more popularity day by day and I also enjoy to work in it,

I will custom js file to all the Angular 7 components programmatically.

Here are the steps you need to follow:

 

1. You need to create new src/custom.js file and add below code:

alert("test");

How to include custom js file to all components in Angular 7

 

 

2. Now add below code into your angular.json file:

...
"scripts": ["src/custom.js"]
...

 

3. After execute your Angular 7 application, you will get alert on every component load and below you can see added file code:

added_custom_js_file

 

If you have any query related to this post, then please do comment below or ask question.

Thank you,

Alisha,

TheRichPost

You may also like

5 comments

sanka December 15, 2018 - 4:48 pm

It was very helpfull

Reply
Ajay Malhotra December 15, 2018 - 4:50 pm

Thank you sanka

Reply
Julien Pitt December 17, 2019 - 7:27 am

Great

Reply
Edi February 28, 2020 - 4:55 pm

Thanks but this won’t work if the above js code is used within in the typescript code in the components, for instance. Your type script code won’t even compile or run.

Reply
Ajay Malhotra February 28, 2020 - 4:58 pm

But this is working for me.

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.