Home  >  Article  >  Web Front-end  >  How to Integrate External JavaScript Functions into an Angular Project?

How to Integrate External JavaScript Functions into an Angular Project?

Barbara Streisand
Barbara StreisandOriginal
2024-10-26 14:23:31956browse

How to Integrate External JavaScript Functions into an Angular Project?

Integrating JavaScript Functions into Angular Projects

Question:

How can I incorporate a JavaScript script file and call a specific function within it, from within an Angular project?

Answer:

To integrate a JavaScript script file into Angular, you can refer to it in your angular-cli.json (or angular.json in Angular 6 ) file. This informs the Angular CLI where to locate the script during build time.

Add the following line to the "scripts" array:

"scripts": [
    "../path-to-script"
]

Next, you need to ensure TypeScript can recognize the external script. Create a typings.d.ts file (if it doesn't exist) in the src directory and add the following declaration:

declare var variableName: any;

In your Angular component or service, you can then import the script as follows:

import * as variable from 'variableName';

Using this technique, you can access the 'public' function (xyz()) from the abc.js script in your Angular code, allowing you to seamless integrate external JavaScript functionality.

The above is the detailed content of How to Integrate External JavaScript Functions into an Angular Project?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn