欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Vue3 使用 reactive 定义的响应式变量 使用计算属性监听该变量并不能实时更新它,您需要定义一个 ref 来做到这一点。

最编程 2024-03-07 17:13:50
...

在 Vue 3 中,如果你使用 reactive 创建响应式对象,然后在 computed 中使用这些响应式对象,确实可能会出现计算属性不会实时更新的情况。这是因为 computed 默认情况下只会在它所依赖的响应式变量被访问时才会重新计算,而不会在这些变量的内部值发生改变时触发重新计算。

let curPaper=ref({
  type: 'other',
  width: 210,
  height: 296.6
});
const curPaperType = computed(() => {
  debugger
  let type = 'other';
  let types = paperTypes;
  for (const key in types) {
    let item = types[key];
    let { width, height } =  curPaper.value;
    if (item.width === width && item.height === height) {
      type = key;
    }
  }
  return type;
});