我已经用 foundation 安装了 angular 6,但我在使用 JQuery 时遇到错误。
Uncaught syntaxerror: unexpected identifier ReferenceError: $ is not defined
在我的组件中,我有
declare var $:any
onNgInit () {
$(document).foundation()
}
我的 angular.json 中有所有导入
scripts : [
"node_modules/jquery/dist/jquery.js"
]
我已经尝试了我在互联网上找到的所有内容,但仍然面临同样的错误。还要添加这个在我使用 Angular 4 时有用的
最佳答案
这与 ES6 模块隔离的工作方式有关。从 angular.json
文件加载与 index.html 文件(全局范围)上的脚本标记相同。当您将它导入任何组件时,您将获得一个新的本地副本,因此请避免以这种方式导入它。
为防止智能感知和编译问题,创建一个包含以下内容的 typings.d.ts
文件:
declare var $: any;
declare var jQuery: any;
现在你可以开始了:)
关于jquery - Angular 6 引用错误 : $ is not defined error with JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50793325/