简体   繁体   中英

How does Angular bind an external javascript library to a declared var?

I'm looking to understand the mechanics of how Angular resolves a declared var to a specific javascript library. For example I reference a 3rd party javascript library and load it through index.html:

<html>
  <head>
    <script src="/assets/js/external.js"></script>
  ...
</html>

SomeComponent.ts

import ...

declare var functionA: any;

@Component({
  selector: 'some-component',
  templateUrl: 'some-component.html',
  styleUrls: ['some-component.css']
})
export class SomeComponent { ... }

does external.js have to have a function declared as functionA , to be resolved correctly? Or does Angular resolve it by matching the signature of functionA when I define the initial call to functionA in SomeComponent? And where can I find this documented - I'm probably not searching correctly.

external.js library can expose some of its objects or functions globally. It means that after the library is loaded you can access the exports from wherever you like on that page with window.exportedFn() or just exportedFn() . it is not usual way to pollute global with your functions, so typescript needs to be told that the exported thing exists and probably provide its signature. if the signature is wrong you won't get any compile time errors, because it is not linked anyhow. TS just believes that the signature is right. You will just get a runtime error if you are using your function in a wrong way

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