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
I want to make a docker for cron scheduled tasks ...
The function has been implemented a long time ago...
Table of contents Preface Child components pass d...
How to solve the problem of being unable to acces...
Table of contents uni-app Introduction HTML part ...
Overview Databases generally execute multiple tra...
Unfortunately, the MYSQL_DATA_TRUNCATED error occ...
Table of contents introduce Installation and Usag...
Preface This article introduces a tutorial on how...
This article is based on MySQL 8.0 This article i...
Copy code The code is as follows: <!-- Prevent...
1. Permanent modification, valid for all users # ...
Table of contents 1. writable: writable 2. enumer...
<br />Previous article: Seven Principles of ...
Today I was asked what the zoom attribute in CSS ...