[英]DOM is not updating when data variable changes in VueJS
I'm trying to update an array of arrays and rendering it as a reactive variable, but DOM is not reflecting those changes.我正在尝试更新数组数组并将其呈现为反应变量,但 DOM 并未反映这些更改。
For doing this, I'm using 2 components, a parent and a child one:为此,我使用了 2 个组件,一个父组件和一个子组件:
Parent component Instanciates child component and passes a method (fetchOptionsFn) as a prop, which fetches some data and updates an array located in child component父组件实例化子组件并将方法(fetchOptionsFn)作为道具传递,该方法获取一些数据并更新位于子组件中的数组
<component :is="DynamicDataPairForm"
:fetchOptionsFn="fetchOptionsFn"
fetchOptionsFn (search, loading, selectOptionsArray, index) {
if (search.length >= 3) {
pptDocumentLibraryService.findByTitle(search).then(res => {
selectOptionsArray[index] = res.data
}).catch(err => console.log(err))
}
Child component Defines an empty array, defines a SELECT in the DOM and when a search on it is done, this array is modified by the parent inherited function.子组件定义一个空数组,在 DOM 中定义一个 SELECT,当对其进行搜索时,该数组被父继承函数修改。
<tr v-for="(entity,index) in filteredEntityList" :key="entity[entityIdField]">
<v-select
:options="selectEntityArray[index]"
@search="(search, loading) => {
fetchSelectEntityOptions(search, loading, index) }"
/>
</tr>
props: {
fetchOptionsFn: { type: Function, required: false, default: () => {} },
},
data: {
selectEntityArray: []
},
methods: {
fetchSelectEntityOptions (search, loading, index) {
this.fetchOptionsFn(search, loading, this.selectEntityArray, index)
}
}
The problem comes when this funcion is called (fetchSelectEntityOptions), but the array (selectEntityArray) doesn't change in DOM.当调用此函数 (fetchSelectEntityOptions) 时会出现问题,但数组 (selectEntityArray) 在 DOM 中没有更改。
Any idea?任何想法? Thanks!
谢谢!
Seems like you might need to define an index
data property in the child component otherwise I'm not sure what/which index
is being referred to in the :options="selectEntityArray[index]"
attribute.似乎您可能需要在子组件中定义
index
数据属性,否则我不确定:options="selectEntityArray[index]"
属性中引用的是什么/哪个index
。 Then you also need to set the index in the fetchSelectEntityOptions function.然后你还需要在 fetchSelectEntityOptions 函数中设置索引。
So, maybe something like this:所以,也许是这样的:
<v-select
:options="selectEntityArray[index]"
@search="(search, loading) => {
fetchSelectEntityOptions(search, loading, index) }"
/>
props: {
fetchOptionsFn: { type: Function, required: false, default: () => {} },
},
data: {
selectEntityArray: [],
index: null,
},
methods: {
fetchSelectEntityOptions (search, loading, index) {
this.index = index;
this.fetchOptionsFn(search, loading, this.selectEntityArray, index);
}
}
Vue 2 has some Change Detection Caveats you should know... Vue 2 有一些你应该知道的变更检测警告......
Vue cannot detect the following changes to an array:
Vue 无法检测到数组的以下更改:
- When you directly set an item with the index, eg
vm.items[indexOfItem] = newValue
当您直接设置带有索引的项目时,例如
vm.items[indexOfItem] = newValue
- When you modify the length of the array, eg
vm.items.length = newLength
当你修改数组的长度时,例如
vm.items.length = newLength
So you need to change the line:所以你需要改变这一行:
selectOptionsArray[index] = res.data
to:至:
Vue.set(selectOptionsArray, index, res.data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.