You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 13项目npm安装jQuery后无法加载的问题求助

在Angular 13中正确安装并全局加载jQuery

问题场景

在Angular 13项目中,因需引入基于jQuery开发的外部依赖,执行npm install jquery后,package.json已包含"jquery": "^3.5.1",node_modules中也已下载jQuery包。但运行ng serve启动项目后,在Chrome控制台输入$().jquery验证时出现报错:

Uncaught TypeError: Cannot read properties of undefined (reading 'jquery') at :1:4

这说明jQuery未被全局加载,浏览器环境无法访问$或jQuery变量。

完整解决方案

1. 安装jQuery及TypeScript类型声明

为避免TypeScript编译报错,需同时安装jQuery本体和对应的类型定义包:

npm install jquery @types/jquery --save

2. 配置全局脚本加载

打开项目根目录的angular.json文件,找到architect > build > options > scripts数组,添加jQuery的本地文件路径:

"scripts": [
  "node_modules/jquery/dist/jquery.min.js"
]

若需要在测试环境也生效,同步更新architect > test > options > scripts数组。

3. 验证加载结果

重启项目:

ng serve

在Chrome开发者工具控制台输入$().jquery,若能正常返回jQuery版本号(如3.5.1),则表示加载成功。

问题更新反馈

将node_modules/jquery/dist/jquery.min.js添加到angular.json的scripts元素后,控制台执行$().jquery已可正常显示版本号,问题解决。

内容的提问来源于stack exchange,提问作者Kingamere

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 13:07:07