Home >Web Front-end >Vue.js >The array in Vue has changed, but the page is not rendered, what should I do?
Solve the problem of the page not rendering after changing the Vue array: Make sure the array is responsive, use Vue.set() or the native array method. Checks whether the array was modified via a reactive method. Use watch or computed listeners to trigger manual rendering. Use Vue.nextTick() to trigger rendering manually. Check the Virtual DOM and recreate the Vue instance.
Solution to the page not rendering after the array changes in Vue
In Vue, when the array changes , the page will usually render automatically. However, sometimes you encounter a situation where the page does not render after the array is changed. Here are a few ways to solve this problem:
1. Make sure the array is reactive
Vue can only track reactive data, so the array must be reactive of. Reactive arrays can be created in the following ways:
Vue.set()
Method: Vue.set(vm.array, index, value)
Array.prototype.push()
, pop()
, shift()
, unshift()
Wait for native array methods2. Check whether the array has been modified
If the array is not modified through a reactive method, Vue cannot detect the change. Make sure modifications to the array use reactive methods or replace the array with a new reactive array.
3. Triggering the listener
can be done by using watch
or computed
to listen for changes in the array and trigger it manually Page rendering.
watch listener: `
javascript
vm.watch('array', (newValue, oldValue) => {
// Manually trigger rendering
})
computed listener: `
javascript
vm. computed({
get() {
return vm.array.length;
}
})
4. Use Vue.nextTick()
Vue.nextTick()
(or $nextTick()
) will queue the callback function into the next DOM Update Cycle. Page rendering can be manually triggered in the callback function.
<code class="javascript">vm.$nextTick(() => { vm.forceUpdate();</code>
<code> **5. 检查 Virtual DOM** 虽然不太常见,但有时 Virtual DOM 可能会丢失数组更改。可以通过销毁 Vue 实例并重新创建它来解决此问题。 </code>
The above is the detailed content of The array in Vue has changed, but the page is not rendered, what should I do?. For more information, please follow other related articles on the PHP Chinese website!