Home > Software engineering >  How to make my TypeScript object available to a third party Javascript
How to make my TypeScript object available to a third party Javascript

Time:08-15

I have an Angular application and need to create an API for a third party javascript that will be added dynamically.

public class MyApi{
   public callSomeFunction():void{...}
   public getSomeValue():any {...}   
}
var publicApi = new MyApi();

How do I get the publicApi object into javascripts global scope?

CodePudding user response:

You can add it to the global object of the client-side which is window object.

window.publicApi = new MyApi();

CodePudding user response:

If by global scope you mean window then: (window as any).publicApi = new MyApi();
If you just want to import it from other packages then export it by adding
export const publicApi = new MyApi();

And then use it depending on which module system you use:
const {publicApi} = require('./path/to/publicApi')
or
import {publicApi} from './path/to/publicApi'

  • Related