nuxtjs项目中,全局鼠标mouseup出现Cannot read property ‘disabled‘ of null

修复Vue组件中el-dropdown的鼠标抬起事件错误
当在Vue应用中使用el-dropdown组件并遇到全局mouseup事件触发的‘disabled’属性读取错误时,解决方案可能是删除无效的组件元素,或者在el-dropdown内添加el-dropdown-menu。此问题通常发生在el-dropdown的item子元素设置不当的情况下。

全局鼠标mouseup出现Cannot read property ‘disabled’ of null

原因

如果你在页面中使用了el-dropdown组件,但是在这个组件里写它的item子元素,就会报这个错误,解决办法就是删除无效的这个组件或者在el-dropdown中添加el-dropdown-menu就可以了。

结尾处,欢迎交流学习,纠错。

这个错误是由于在获取 `modalRef.current` 的 `offsetLeft` 属性时,`modalRef.current` 的值为 `null` 引起的。这通常是因为在组件渲染之初获取 `modalRef.current` 时,可能还未完成 Modal 的挂载。 为了解决这个问题,可以使用 `useEffect` 钩子来确保在获取 `modalRef.current` 时 Modal 已经挂载。修改代码如下: ```jsx import { Modal } from &#39;antd&#39;; import { useState, useRef, useEffect } from &#39;react&#39;; const DraggableModal = ({ visible, title, onCancel, children, }) => { const [dragging, setDragging] = useState(false); const modalRef = useRef(); useEffect(() => { if (modalRef.current) { modalRef.current.style.left = &#39;100px&#39;; modalRef.current.style.top = &#39;100px&#39;; } }, []); const handleMouseDown = (e) => { e.stopPropagation(); setDragging(true); document.addEventListener(&#39;mousemove&#39;, handleMouseMove); document.addEventListener(&#39;mouseup&#39;, handleMouseUp); }; const handleMouseMove = (e) => { if (!dragging) return; const modalDOM = modalRef.current; modalDOM.style.left = `${modalDOM.offsetLeft + e.movementX}px`; modalDOM.style.top = `${modalDOM.offsetTop + e.movementY}px`; }; const handleMouseUp = () => { setDragging(false); document.removeEventListener(&#39;mousemove&#39;, handleMouseMove); document.removeEventListener(&#39;mouseup&#39;, handleMouseUp); }; return ( <Modal visible={visible} title={title} onCancel={onCancel} mask={false} ref={modalRef} footer={null} > <div onMouseDown={handleMouseDown} style={{ cursor: &#39;move&#39; }} > {children} </div> </Modal> ); }; export default DraggableModal; ``` 在上述代码中,我们使用 `useEffect` 钩子来在组件挂载后初始化 Modal 的位置。在 `useEffect` 的回调函数中,我们检查 `modalRef.current` 是否存在,如果存在则设置初始位置为 `left: &#39;100px&#39;` 和 `top: &#39;100px&#39;`。 这样就可以确保在获取 `modalRef.current` 的属性之前,Modal 已经成功挂载,避免了 `TypeError: Cannot read properties of null` 错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值