繁体   English   中英

无法从TS文件访问外部JS文件的功能

[英]Not able to access a function of external JS file from TS file

我已经在外部JS文件中定义了一个函数,但无法在home.page.ts文件中使用它。 它显示为TypeError错误:无法读取未定义的属性'functionName'

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.

请帮忙。

将您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM