vue3使用watch失效的一个原因

1 篇文章 0 订阅
1 篇文章 0 订阅

在使用vue3时,如果发现watch监听未生效,可能是以下这个原因

在控制台报了如下一个watch警告

runtime-core.esm-bundler.js:40

[Vue warn]: Invalid watch source: undefined A watch source can only be a getter/effect function, a ref, a reactive object, or an array of these types.

at <AddUpBrandVoucher modelValue=false onUpdate:modelValue=fn ref="addUpRef" ... >

at <BrandVoucher class="router-view" onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< Proxy {__v_skip: true} > ... >

at <KeepAlive key=1 max=10 >

at <BaseTransition appear=false persisted=false mode=undefined ... >

at <Transition name="fade" >

at <RouterView class="router-view" >

at <ElMain style= {background: '#f3f4f6'} >

at <ElContainer>

at <ElContainer>

at <Admin onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< Proxy {__v_skip: true} > >

at <RouterView>

at <App>

这种原因是因为watch里面监听的值的类型不符合要求,watch监听的值要是以下几个类型:

  1. 函数类型,该函数返回一个值

  1. ref对象

  1. reactive对象

  1. 以上数据类型的数组

如果要监听ref对象中的某个值,要用第一种方式

例如图中,需要监听的值是一个ref对象里面的String类型的值,红框中监听值的写法是不对的,需要将监听值写成黄色框中的样子,以一种函数返回值书写。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值