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

WeChat applet implements video player sending bullet screen

This article shares the specific code for WeChat ...

Detailed explanation of Linux mpstat command usage

1. mpstat command 1.1 Command Format mpstat [ -A ...

Nginx routing forwarding and reverse proxy location configuration implementation

Three ways to configure Nginx The first method di...

HTML table tag tutorial (19): row tag

The attributes of the <TR> tag are used to ...

MySQL data type optimization principles

MySQL supports many data types, and choosing the ...

Some slightly more complex usage example codes in mysql

Preface I believe that the syntax of MySQL is not...

Linux gzip command compression file implementation principle and code examples

gzip is a command often used in Linux systems to ...

Detailed explanation of the pitfalls of MySQL 8.0

I updated MySQL 8.0 today. The first problem: Nav...

How to use Linux tr command

01. Command Overview The tr command can replace, ...

Web Design Tutorial (8): Web Page Hierarchy and Space Design

<br />Previous article: Web Design Tutorial ...

In-depth study of JavaScript array deduplication problem

Table of contents Preface 👀 Start researching 🐱‍🏍...

Detailed explanation of CSS sticky positioning position: sticky problem pit

Preface: position:sticky is a new attribute of CS...