javascript - 组件中的Vuejs3 react 数组

标签 javascript vue.js vuejs3 vue-composition-api

我尝试在组件中使用响应式(Reactive)数组。
它适用于对象,但不适用于对象数组。
数组更新时如何更新 View ?

var self = currentClassInstance // this

self.store = {
    resources: Vue.reactive([]),
    test:  Vue.reactive({ test: 'my super test' }),

    setResources(resources) {
        // this one doesn't update the view. ?
        this.resources = resources

    }, 
    setResources(resources) {
        // this one update the view
        this.test.test = "test ok"
    },  
}


....

const app_draw = {
    data() {
        return {
            resources: self.store.resources,
            test: self.store.test,
        }
    },
       
    updated() {
        // triggered for "test" but not for "resources"
        console.log('updated')
    },
       
    template: '<div v-for="(resource, key) in resources" :data-key="key">{{resource.name}}</div>'
};
....

最佳答案

根据official docs :

Reactive:
Takes an object and returns a reactive proxy of the original. This is equivalent to 2.x's Vue.observable()
....
The reactive conversion is "deep": it affects all nested properties. In the ES2015 Proxy based implementation, the returned proxy is not equal to the original object. It is recommended to work exclusively with the reactive proxy and avoid relying on the original object.


我建议像使用 test 一样将数组分配给 react 参数中名为 value 的字段:
resources: Vue.reactive({value:[]}),
然后使用 resources.value=someVal更新该值。

关于javascript - 组件中的Vuejs3 react 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64416605/

相关文章:

javascript - 如何在循环中绑定(bind)点击样式

javascript - 在 Redux 中处理长生命周期状态

javascript - v-for 在每个函数执行时

docker - Vue Vite 无法连接docker容器

javascript - 如何通过 vue-svg-loader 在 vue.js 中导入多个 svg

vue.js - 如何让Vue 3渲染函数h()输出html

javascript - 将 for 循环中每个项目的值设置为 true 或 false

javascript - 在 Bootstrap 3 SlideDown() 隐藏元素上有问题

javascript - PHP 将文件文本附加到所有 JavaScript 文件

javascript - 如何使用 JSON 中的选项构建 optgroup?