Project scenario:There is a <ul> tag on the page display. We need to display the list data dynamically. In addition to the list values, there are other values to be displayed on our page. Therefore, the data structure of the list data is an array under an object. After dynamically modifying the data, it is found that it is not automatically rendered. Problem description: When you click the "click me!" button, the data changes and is output to the console, but the list data is not rendered. <script src="https://unpkg.com/vue/dist/vue.js"></script> <div id="app"> <button @click="pushDataToDataList">click me!</button> <ul> <li v-for="(item, i) in form.dataList" :key="item"> {{ i + ":" + item }} </li> </ul> </div> <script> let app = new Vue({ data: function() { return { form: {} } }, methods: { pushDataToDataList() { if (this.form.dataList == null) { this.form.dataList = [] } this.form.dataList.push("abc" + this.form.dataList.length) console.log(this.form.dataList) } } }).$mount('#app') </script> Cause Analysis:After consulting the official documentation, we found the following passage
The reason is clear here. Our data is not rendered because at the beginning, there is no
Solution: 1. In the form object under data, set data: function() { return { form: { dataList: null } } } 2. Use this.$set() method pushDataToDataList() { if (this.form.dataList == null) { // First set the dataList property under form this.$set(this.form, 'dataList', []) } this.form.dataList.push("abc" + this.form.dataList.length) console.log(this.form.dataList) } Reference LinksNotes on Vue's detection of data changes This is the end of this article about how to solve the problem that Vue bound objects and array variables cannot be rendered after changes. For more related content about the problem that Vue bound objects and array variables cannot be rendered after changes, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: How to install MySQL 8.0.13 in Alibaba Cloud CentOS 7
Table of contents background Understanding compos...
This article example shares the specific code of ...
AWS - Amazon's cloud computing service platfo...
As the number of visits increases, for some time-...
Phenomenon The system could compile the Linux sys...
Samba Services: This content is for reference of ...
Keepalived installation: cd <keepalived_source...
Vulnerability Description Apache Flink is an open...
A root routing component (the root routing compon...
When using justify-content:space-between layout, ...
Introduction: In many cases, many people think th...
Table of contents Bidirectional binding principle...
This article will introduce how to query data in ...
First, setInterval is encapsulated as a Hook 👇 im...
1. unlink function For hard links, unlink is used...