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

求助:为何持续触发Uncaught TypeError: Cannot read properties of undefined (reading 'fn')

错误排查:Uncaught TypeError: Cannot read properties of undefined (reading 'fn')

这个错误的核心原因是代码中$.fn为undefined,说明$(即jQuery)没有被正确加载或引入,导致无法访问jQuery的原型方法对象。结合你提供的代码片段,按以下步骤排查:

1. 检查jQuery的安装与导入

  • 确认项目已安装jQuery:执行npm install jquery(使用npm/yarn时),或者通过CDN引入jQuery脚本(需放在CardRefresh.js之前)。
  • 验证导入语句:你的代码用了import $ from 'jquery',确保在模块环境(如Webpack、Vite)中配置正确,jQuery能被正确解析为模块。如果是传统非模块环境,需改用全局引入,比如:
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script src="path/to/CardRefresh.js"></script>
    

2. 确认加载顺序

  • 如果是多脚本引入场景,必须保证jQuery脚本在CardRefresh.js之前加载,否则当CardRefresh执行时,$还未被定义。

3. 排查版本兼容性

  • 确认AdminLTE版本与jQuery版本匹配:AdminLTE 3.x通常要求jQuery 3.x以上版本,避免使用过旧的jQuery版本导致模块导入或全局对象异常。

4. 临时调试验证

在代码开头添加调试语句,确认$是否存在:

import $ from 'jquery'

// 调试:打印$的值
console.log('jQuery instance:', $)
console.log('$.fn exists:', !!$.fn)

如果打印结果中$为undefined,说明导入完全失败;如果$存在但$.fn不存在,可能是引入了非标准的jQuery变体(比如某些精简版)。


附你提供的代码片段(格式化后):

/** --------------------------------------------
 * AdminLTE CardRefresh.js
 * License MIT
 * --------------------------------------------
 */

import $ from 'jquery'

/**
 * Constants
 * ====================================================
 */

const NAME = 'CardRefresh'
const DATA_KEY = 'lte.cardrefresh'
const EVENT_KEY = `.${DATA_KEY}`
const JQUERY_NO_CONFLICT = $.fn[NAME]

const EVENT_LOADED = `loaded${EVENT_KEY}`
const EVENT_OVERLAY_ADDED = `overlay.added${EVENT_KEY}`
const EVENT_OVERLAY_REMOVED = `overlay.removed${EVENT_KEY}`

const CLASS_NAME_CARD = 'card'

const SELECTOR_CARD = `.${CLASS_NAME_CARD}`
const SELECTOR_DATA_REFRESH = '[data-card-widget="card-refresh"]'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 19:45:21