【问题标题】:Not able to access a function of external JS file from TS file无法从 TS 文件访问外部 JS 文件的功能
【发布时间】: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


    【解决方案1】:

    将您的 js 文件移动到资产文件夹,然后在您的代码中这样做

    import 'assets/js/multiLayerSource';
    
    declare var SetLayerHT: any;
    
    ngOnInit() {
     this.layersHTP.push( 'a', 'b', 'c', 'd' );
     SetLayerHT(this.layersHTP);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-24
      • 2012-06-09
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      相关资源
      最近更新 更多