【发布时间】:2019-12-20 08:23:28
【问题描述】:
我正在尝试从 Angular 组件调用 .js 文件中的函数,但我收到错误“错误 ReferenceError:myTest 未在 TechnologiesComponent.onClick 中定义”。
我已按照here 中描述的步骤进行操作,因此我在我的 src 文件夹中创建了一个名为 custom.js 的文件。
该文件包含以下内容:
function myTest() {
alert('Welcome to custom js');
}
$(function() {
alert('Hello, custom js');
});
我已经在我的 angular.json 中的脚本数组中添加了脚本,如下所示:
"tsConfig": "tsconfig.app.json",
"aot": false,
"assets": [
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": ["src/custom.js"]
},
我要在其中使用 .js 文件的 .ts 文件如下所示:
import { Component } from '@angular/core';
declare const myTest: any;
@Component({
selector: 'app-technologies',
templateUrl: './technologies.component.html',
styleUrls: ['./technologies.component.css']
})
export class TechnologiesComponent {
onClick() {
myTest();
}
}
我的模板中添加了一个按钮: 点我
按下按钮时,会抛出错误“ERROR ReferenceError: myTest is not defined at TechnologiesComponent.onClick”。为什么?
【问题讨论】:
-
不知道
declare const myTest: any;这个custom.js的参考怎么样?你忘记使用import了吗?例如:stackoverflow.com/questions/44817349/…
标签: javascript angular