Home > Enterprise >  How to include custom JS file in bundle Angular?
How to include custom JS file in bundle Angular?

Time:05-05

Now I use directrly embedded link to JS file in index.html:

 <script sync src="http://api.js" type="text/javascript"></script>

How to add this custom js file in bundle Angula from config file?

CodePudding user response:

Why do you need this? Do you need this in every component for example? What is api.js for?

Probably you should go the Angular Way and create a Service, which than can be used in your components. Angular.io has quite some good sources on how to work with components and services. Introduction to services: https://angular.io/tutorial/toh-pt4

CodePudding user response:

I don't know WHAT you would use it for, but I assume you're doing a replace of urls on that file? Whatever the reason, you can add js files why the assets array in the angular.json

    ...
    "assets": [
      "src/favicon.ico",
      "src/assets",
      "src/api.js" <- your file
    ],
  • Related