我有一个利用 SignalR 与桌面应用程序通信的应用程序。要使用 SignalR,我需要在我的 .ts 文件中使用 jQuery。但是,从 Angular 7 迁移到 Angular 8 后,它似乎不起作用。
我用 declare var $: any;
就像我在以前版本的 Angular 中一样。不幸的是, $ 现在在控制台打印空白。
那么,Angular v8 不再支持以这种方式使用 jQuery,还是在迁移过程中出现了其他问题?
更新:
我通过 npm 加载了 jQuery v3.3.1。
这使它成为全局(在 angular.json 中)
"scripts": [
"./node_modules/jquery/dist/jquery.min.js",
"./node_modules/signalr/jquery.signalR.js"
]
最佳答案
不使用更优雅的方式
declare var $: any;
第一次运行
npm install jquery --save
npm install @types/jquery --save
然后在架构师的脚本部分 => 构建 angular.json 文件添加 jquery lib 的路径
"scripts": [
"node_modules/jquery/dist/jquery.min.js"
]
然后在你的 tsconfig.app.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"types": ["jquery"] // add here
},
"exclude": ["test.ts", "**/*.spec.ts"]
}
所以现在你可以在项目中的任何地方使用 jquery,而无需使用
declare var $ : any
。对于您需要使用 jquery 的每个文件
关于javascript - 如何在 Angular 8 应用程序中使用 jQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56941924/