我有一个文件,其中包含几十个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
}
}
使用import加载external.js,在ngAfterViewInit()中我想调用external.js中的init()函数,该函数会调用该外部文件中的所有其他方法。
以下是external.js的部分内容:
function init() {
callFunction1();
callFunction2();
}
return { init: function() { init(); } }
})(webGlObject||{})并在
ngAfterViewInit()中尝试使用
webGlObject.init();访问,但是出现了错误
errors.service.ts:29 ReferenceError: webGlObject is not defined`。 - Haris Hajdarevic