【发布时间】:2020-01-11 22:43:09
【问题描述】:
我在外部 JS 文件中定义了一个函数。我无法在 home.page.ts 文件中使用它。它显示错误为 TypeError: Cannot read property 'functionName' of undefined
index.html
<body>
<app-root></app-root>
<script src="assets/multiLayerSource.js"></script>
</body>
multiLayerSource.js
var multiLayerSource;
var layersHT = [];
function SetLayerHT(arglayersHT) {
layersHT = arglayersHT;
}
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);
}
}
尝试访问 SetLayerHT() 时显示错误。 错误是:
TypeError: Cannot read property 'SetLayerHT' of undefined.
请帮忙。
【问题讨论】:
标签: javascript node.js angular typescript ionic-framework