[Vue warn]: Unhandled error during execution of native event handler

问题出现:

在切换路由的时候出现一堆报错:

[Vue warn]: Unhandled error during execution of native event handler 
  at <Index onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< null > > 
  at <BaseTransition mode="out-in" appear=false persisted=false  ... > 
  at <Transition name="toggle" mode="out-in" > 
  at <RouterView> 
  at <App>
Uncaught TypeError: Cannot read properties of null (reading 'getBoundingClientRect')
    at handleMouseMove (index.vue:86:30)
    at callWithErrorHandling (chunk-2FDUVFJ5.js?v=729ecf8b:1660:19)
    at callWithAsyncErrorHandling (chunk-2FDUVFJ5.js?v=729ecf8b:1667:17)
    at HTMLDivElement.invoker (chunk-2FDUVFJ5.js?v=729ecf8b:10287:5)

问题分析:

经过仔细查看报错信息,排查问题出现在:

<RouterView v-slot:="{Component}">
    <Transition name="toggle" mode="out-in">
        <component :is="Component"></component>
    </Transition>
</RouterView>

和某个事件处理器之间的冲突,而的这个第二个报错说明是在使用了 getBoundingClientRect  的函数时候的报错.

立马就定位到了这里:

<div id="index-container" @mousemove="handleMouseMove">
</div>

const handleMouseMove = (event)=> {
...
    getBoundingClientRect()
...
}

突然想到,会不会是因为监听的事件没有移除导致的错误,然后查了查,没有@eventHandler 的移除函数,就采用最传统的方法.

问题解决

onMounted(()=>{
    window.addEventListener('mousemove', handleMouseMove)
})
onBeforeUnmount(() => {
    window.removeEventListener('mousemove', handleMouseMove);
})

这样便解决了.

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值