简体   繁体   English

更改数组时Vue3 ref不更新

[英]Vue3 ref not updating when the array is changed

This issue occurs only when I'm trying to update an array which uses Vue's "ref".仅当我尝试更新使用 Vue 的“ref”的数组时才会出现此问题。

Here's some basic example code of what I have:这是我所拥有的一些基本示例代码:

<script setup>
import { ref } from 'vue';

let arr = ref([]);

function someUpdateFunc(val) {
    arr.value.push(val);

    console.log(arr.value);
}
</script>

If I check the console in my browser I see the updated array as expected.如果我在浏览器中检查控制台,我会按预期看到更新后的数组。 However, when I try to run some functionality in the same component that needs the updated values it doesn't work.但是,当我尝试在需要更新值的同一组件中运行某些功能时,它不起作用。

To confirm this I installed the Vue dev tools Chrome extension and checked the array on the component.为了确认这一点,我安装了 Vue 开发工具 Chrome 扩展并检查了组件上的数组。 It matches the functionality not working as the variable never updates and looks like this:它与不工作的功能相匹配,因为变量永远不会更新,如下所示:

arr: Array[0] (Ref)

Please can anyone advise why my array isn't updating?请谁能告诉我为什么我的阵列没有更新?

Thanks,谢谢,

try use this.$set?尝试使用 this.$set?

this.$set("Object","Properties of Object","changeValue") this.$set("对象","对象的属性","changeValue")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM