gpt4 book ai didi

javascript - 从js文件导入的函数,不在TS文件中运行

转载 作者:行者123 更新时间:2023-11-28 17:00:01 24 4
gpt4 key购买 nike

我试图在 TS 文件的组件内调用 JS 函数,但遇到异常。

组件

import '../../../assets/js/gantt/ganttMaster.js';
export class TaskComponent implements OnInit {

constructor() {}

ngOnInit() {
var r = new GanttMaster();
}
}

错误:

Error referecences error: GanttMaster is not defined

最佳答案

您需要更改导入 .js 文件的方式:

import * as gantt from '../../../assets/js/gantt/ganttMaster.js';
export class TaskComponent implements OnInit {

constructor() {}

ngOnInit() {
var r = new gantt.GanttMaster();
}
}

如果要在多个组件之间使用GanttMaster,可以在angular.json中导入.js文件,并在app.module.ts 就像声明 const GanttMaster: any 一样。然后您可以在您的应用程序中使用它。

希望这有帮助。

<小时/>

更新

或者,您可以按照已经完成的方式导入它,但在导入之前手动声明该函数:

declare const GanttMaster: any;
import from '../../../assets/js/gantt/ganttMaster.js';
export class TaskComponent implements OnInit {

constructor() {}

ngOnInit() {
var r = new GanttMaster();
}
}

引用号:https://stackoverflow.com/a/37084553/1331040

关于javascript - 从js文件导入的函数,不在TS文件中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57782529/

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