Solution to Vue's inability to watch array changes

Solution to Vue's inability to watch array changes

1. Vue listener array

Vue can actually monitor array changes, such as

data () {
  return {
    watchArr: [],
  };
},
watchArr (newVal) {
  console.log('Listening: ' + newVal);
},
created () {
  setTimeout(() => {
    this.watchArr = [1, 2, 3];
  }, 1000);
},

For example, use splice(0,2,3) to delete two elements from array index 0 and insert an element 3 at index 0.

data () {
  return {
    watchArr: [1, 2, 3],
  };
},
watchArr (newVal) {
  console.log('Listening: ' + newVal);
},
created () {
  setTimeout(() => {
    this.watchArr.splice(0, 2, 3);
  }, 1000);
},

The push array can also be monitored.

2. Situations where vue cannot monitor array changes

However, arrays cannot be monitored in the following two situations

  • When setting an array item directly using an index, for example, arr[indexofitem]=newValue
  • When modifying the length of an array, for example, arr.length = newLength

Example of a situation where array changes cannot be monitored

1. Use indexes to modify array values ​​directly

data () {
  return {
    watchArr: [{
      name: 'krry',
    }],
  };
},
watchArr (newVal) {
  console.log('Listening: ' + newVal);
},
created () {
  setTimeout(() => {
    this.watchArr[0].name = 'xiaoyue';
  }, 1000);
},

2. Modify the length of the array

  • If the length is greater than the original array, the subsequent elements are set to undefined
  • If the length is less than the original array, the extra elements will be truncated.
data () {
  return {
    watchArr: [{
      name: 'krry',
    }],
  };
},
watchArr (newVal) {
  console.log('Listening: ' + newVal);
},
created () {
  setTimeout(() => {
    this.watchArr.length = 5;
  }, 1000);
},

This is the end of this article about Vue not being able to watch array changes. For more related content about Vue not being able to watch array changes, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Details of watch monitoring properties in Vue
  • Detailed explanation of the basic usage of VUE watch listener
  • Reasons and solutions for multiple executions of the watch method when Vue monitors route changes
  • Vue watch about property monitoring in objects
  • Summary of Vue watch monitoring methods

<<:  Detailed explanation of Docker usage under CentOS8

>>:  Reasons and optimization solutions for slow MySQL limit paging with large offsets

Recommend

Understand CSS3 FlexBox elastic layout in 10 minutes

Basic Introduction Features Flexbox is a CSS disp...

Xhtml special characters collection

nbsp &#160; no-break space = non-breaking spa...

JavaScript imitates Jingdong carousel effect

This article shares the specific code for JavaScr...

About the "occupational disease" of designers

I always feel that designers are the most sensiti...

Vue integrates PDF.js to implement PDF preview and add watermark steps

Table of contents Achieve results Available plugi...

Example of using MySQL to count the number of different values ​​in a column

Preface The requirement implemented in this artic...

How to find identical files in Linux

As the computer is used, a lot of garbage will be...

How to detect if the current browser is a headless browser with JavaScript

Table of contents What is a headless browser? Why...

A brief discussion on MySQL count of rows

We are all familiar with the MySQL count() functi...

How to use the Linux more command in Linux common commands

more is one of our most commonly used tools. The ...

Should the Like function use MySQL or Redis?

Table of contents 1. Common mistakes made by begi...