根据代码,可能是因为在代码中没有定义odds的初始值,因此在第一次执行watch时会报错。可以尝试将odds的初始值设为0或其他默认值,即将以下代码:

const odds = ref(1)

改为:

const odds = ref(0)const odds = ref(null)

另外,也可以在watch中加入判断,当odds为undefined时不执行代码,避免报错。可以将以下代码:

watch(dog_qd, async (newDog_qd, oldDog_qd) => {
  // ...
})

改为:

watch(dog_qd, async (newDog_qd, oldDog_qd) => {
  if (odds.value === undefined) {
    return
  }
  // ...
})
Vue.js 代码报错:odds is undefined - 解决方法

原文地址: https://www.cveoy.top/t/topic/lEBH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录