外部JavaScript文件在angular 2组件内部无效。它显示错误myEObject is not defined
。
<p id="demo">A Paragraph.</p>
<button type="button" (click)="callFun()">Try it</button>
import { Component } from '@angular/core';
import '../myExternal.js';
declare var myEObject: any;
@Component({
selector: 'external-file',
templateUrl: './external.component.htmll'
})
export class ExternalComponent {
callFun(){
myEObject.myFunction();
}
}
var myEObject = (function() {
return {
func1: function() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
}
})(myEObject||{})
答案 0 :(得分:0)
通过终端---
执行此命令npm install jquery --save,
npm install --save-dev @types/jquery
将myExternal.js文件移至资源文件夹