[英]Not able to access a function of external JS file from TS file
I have defined a function in external JS file.I am not able to use it in home.page.ts file. 我已经在外部JS文件中定义了一个函数,但无法在home.page.ts文件中使用它。 It is showing error as TypeError: Cannot read property 'functionName' of undefined 它显示为TypeError错误:无法读取未定义的属性'functionName'
index.html index.html
<body>
<app-root></app-root>
<script src="assets/multiLayerSource.js"></script>
</body>
multiLayerSource.js multiLayerSource.js
var multiLayerSource;
var layersHT = [];
function SetLayerHT(arglayersHT) {
layersHT = arglayersHT;
}
home.page.ts home.page.ts
import { Component } from '@angular/core';
import { OnInit, Renderer, ViewChild } from '@angular/core';
declare var multiLayerSource: any;
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit {
layersHTP: any = [];
constructor() {}
ngOnInit() {
this.layersHTP.push( 'a', 'b', 'c', 'd' );
multiLayerSource.SetLayerHT(this.layersHTP);
}
}
It is showing error while trying to access SetLayerHT(). 尝试访问SetLayerHT()时显示错误。 The error is : 错误是:
TypeError: Cannot read property 'SetLayerHT' of undefined.
Please help. 请帮忙。
Move your js file to asset folder then in your code do like this 将您的js文件移至资产文件夹,然后在代码中执行以下操作
import 'assets/js/multiLayerSource';
declare var SetLayerHT: any;
ngOnInit() {
this.layersHTP.push( 'a', 'b', 'c', 'd' );
SetLayerHT(this.layersHTP);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.