[英]How does Angular bind an external javascript library to a declared var?
我想了解 Angular 如何将声明的 var 解析为特定的 javascript 库的机制。 例如,我引用了一个第三方 javascript 库并通过 index.html 加载它:
<html>
<head>
<script src="/assets/js/external.js"></script>
...
</html>
一些组件.ts
import ...
declare var functionA: any;
@Component({
selector: 'some-component',
templateUrl: 'some-component.html',
styleUrls: ['some-component.css']
})
export class SomeComponent { ... }
external.js 是否必须将 function 声明为functionA
才能正确解析? 还是Angular在我在SomeComponent中定义对functionA的初始调用时通过匹配functionA
的签名来解决它? 我在哪里可以找到这个记录 - 我可能没有正确搜索。
external.js
库可以全局公开它的一些对象或函数。 这意味着在加载库后,您可以使用window.exportedFn()
或exportedFn()
从该页面上您喜欢的任何位置访问导出。 用你的函数污染全局不是通常的方法,所以需要告诉 typescript 导出的东西存在并且可能提供它的签名。 如果签名错误,你不会得到任何编译时错误,因为它无论如何都没有链接。 TS只是认为签名是对的。 如果您以错误的方式使用 function,您只会收到运行时错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.