gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:52:59 25 4
gpt4 key购买 nike

我在外部 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 文件移动到 asset 文件夹,然后在你的代码中这样做

import 'assets/js/multiLayerSource';

declare var SetLayerHT: any;

ngOnInit() {
this.layersHTP.push( 'a', 'b', 'c', 'd' );
SetLayerHT(this.layersHTP);
}

关于javascript - 无法从 TS 文件访问外部 JS 文件的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57866310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com