You can do this using OpaqueToken in Angular2
1 . Create the token that is used to search for the instance, as shown below in a separate ts file.
import { OpaqueToken } from '@angular/core' export let name_of_The_Token = new OpaqueToken('name_Of_The_Window_Object');
2. In App.module, you need to import and declare a variable , which is the name of your window object, which makes Token a service of Angular2 so that you can use the properties, methods in this javascript file for your components.
import { name_of_The_Token } from '/* file_Path */'; declare let name_Of_The_Window_Object : any;
Step 3: Insert into the array of suppliers of your module.
{ provide : name_of_The_Token , useValue : name_Of_The_Window_Object }
Guide to using this token in components
Import the token just like any other service, and @Inject from angular -core
import { name_of_The_Token } from '/* file_Path */'; import { Inject } from '@angular/core';
In the component constructor
constructor(@Inject( name_of_The_Token ) private _serviceObject : any )
Any where in your component you can use the variables and methods of your javascript file as
this._serviceObject.method1() this._serviceObject.variable1 .....
Note One of the disadvantages is that you will not get intellisense .
Overcoming this: If you are looking for intellisense, you need to wrap the methods and variables inside the interface and use it in the type ** (instead of any) ** of your token as
export interface myCustom { method1(args): return_Type; method2(args): void; ..... }
source share