简体   繁体   中英

How do you gain access to global variables and functions from Javascript scripts imported from index.html and use them in a component?

I am using Angular 6 and I have a private JavaScript library I need to use in the project. I am importing it in the index.html file. The project uses globally spaced functions. How do I access them inside of a component. Angular does not gives me an error and does not recognize them.

index.html
.....
<script src="https://somefile.js type="text/javascript" charset="utf-8">
.....

// has a function 
doXYZ()  in the global namespace


somecomponent.ts
......

ngOnInit() {
    doXYZ()  //try to call globally imported function
}
......

ERROR: Cannot find name doXYZ when using ng serve

You may be able to access the global namespace on the window object.

Try accessing your desired function like this:

window['doXYZ']();

window is globally accessible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM