Home >Web Front-end >Front-end Q&A >Vue object changes do not render
Preface
Vue.js is a popular JavaScript library that provides a simple and easy-to-use framework and provides users with an efficient data binding and component programming method. However, when we develop using Vue.js, we may encounter some problems. One of the common problems is that when the data object of the component changes, the component is not re-rendered, resulting in no change in the interface. In this article, we will explore the causes of this problem and how to solve it.
Body
The core idea of Vue.js is data-driven view. When data objects change, the view automatically updates. This is achieved through Vue.js’ reactive system. Vue.js adds all data objects into a reactive system at component definition time. When the properties of a data object change, the reactive system automatically detects the change and notifies the view to update.
However, sometimes we encounter the problem that when the properties of the data object change, the view is not re-rendered. In this case, we need to carefully analyze the cause of the problem and find a solution.
First of all, we need to make it clear that when we modify the properties of an object, Vue.js will only detect the direct properties of the object. When we add a new property to the object, Vue.js will not detect the change in the property. This is because the newly added properties are not added to the reactive system.
For example, in the following code, we add a new property newProp
to an object:
const vm = new Vue({ data: { obj: { prop1: 'value1', prop2: 'value2' } } }) vm.obj.newProp = 'new value'
Because newProp
has not been added to In a responsive system, when we modify the value of newProp
, the view will not re-render.
In order to solve this problem, we can use the $set
method provided by Vue.js to add new properties to the responsive system:
vm.$set(vm.obj, 'newProp', 'new value')
In this way, when we When the value of newProp
is modified, the view will automatically re-render.
In addition to the new properties not being detected, similar problems will also occur when we directly modify the elements in the array. For example, in the following code, we directly modify the elements in the array arr
:
const vm = new Vue({ data: { arr: ['elem1', 'elem2', 'elem3'] } }) vm.arr[1] = 'new elem2'
Since Vue.js does not detect changes in the elements in the array, the view will not re-render. .
In order to solve this problem, we can use the $set
method provided by Vue.js to replace the original element with the modified element:
vm.$set(vm.arr, 1, 'new elem2')
In this way, when we The view will also automatically re-render when the elements in the array are modified.
In addition, we also need to note that in some cases, we may need to manually force the view to be updated. For example, when we use computed properties or listeners, the view does not update automatically when the value in the computed property or listener changes. We need to use the $forceUpdate
method to force the view to be updated:
const vm = new Vue({ data: { value: 1 }, computed: { computedValue () { return this.value + 1 } }, methods: { update () { this.value++ this.$forceUpdate() } } })
Summary
During the Vue.js development process, when we encounter changes in the component data object, the component When there is no re-rendering problem, we need to consider the following points:
$set
Method to add them to a reactive system;$forceUpdate
method forces the view to be updated. The above methods can help us solve the problem of component data objects not rendering when they change. But we still need to follow the principles of Vue.js’s responsive system and make full use of the convenience it brings. If you encounter a problem, carefully analyze its cause and find a solution instead of avoiding the problem directly.
The above is the detailed content of Vue object changes do not render. For more information, please follow other related articles on the PHP Chinese website!