javascript - 如何在 Angular 8 应用程序中使用 jQuery?

标签 javascript node.js angular typescript angular8

我有一个利用 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/

相关文章:

angular - karma 无法识别 Angular Directive(指令)

javascript - 从 jquery/javascript 函数获取数组

javascript - 为什么我的文本不是 h3?

javascript - JQuery:这两个 $.get() 方法有什么区别?

mysql - 在 node.js 中使 MySQL 的 ORDER BY 动态化

node.js - 这是在平均堆栈上进行数据库搜索的最佳方法

javascript - ng build --prod 给我一个错误

javascript - 通过 JavaScript 将两个按钮添加到一个 div

node.js - Mongoose 获取引用模型的_id,无论是否填充

angular - 使用 angular2 以 DD/MM/YYYY 格式验证日期模式