Subscribe on changes!

can't watch object change when changing value in self funtion

avatar
Sep 26th 2021

Version

3.2.19

Reproduction link

sfc.vuejs.org/

Steps to reproduce

can't watch object change when changing value in self funtion 在内部方法中改变变量值,watch和computed无效

const a:any = ref(null)

const test = () => {
  const a :any= {}
  a.time = 0
  a.setValue = function () {
    a.time = a.time + 1
  }  
  return a
}

a.value = test()

onMounted(() => {
  setTimeout(() => {
    a.value.setValue()
  },2000)

})

watch(() => a.value,() => {
  console.log('value change');
},{
  deep:true,
})

What is expected?

log 'value change'

What is actually happening?

log nothing

avatar
Sep 26th 2021
const test = () => {
  const a :any= {
    time: 0,
    setValue(){
      this.time += 1
    }
  }
  return a
}

尝试这么写呢?

avatar
Sep 26th 2021

Manipulation of the original object instead of the reactive proxy can't be tracked.