我有一个文件,有几十个javascript函数.我想要做的是将该文件导入Angular 2组件并运行在“external.js”文件中定义的init()函数.
import { Component, AfterViewInit } from '@angular/core';
import "../../../../assets/external.js";
@Component({
selector: 'app-component',
templateUrl: 'app.component.html'
})
export class ComponentNameComponent implements AfterViewInit {
constructor() { }
ngAfterViewInit(): void {
// invoke init() function from external.js file
}
}
external.js使用import和ngAfterViewInit()加载我想调用external.js中的init()函数,该函数调用该外部文件中的所有其他方法.
这是external.js的一部分:
function init() {
callFunction1();
callFunction2();
}
解决方法:
您可以导入和声明外部对象.之后,您可以在组件中使用它.
import 'external.js'
declare var myExtObject: any;
我在plunker中做了一个例子:
https://plnkr.co/edit/4CrShwpZrDxt1f1M1Bv8?p=preview
希望这可以帮助.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。