我尝试在组件中使用响应式(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'sVue.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/