JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

When watching an object in Vue, how to exclude certain properties from being watched

Author:JIYIK Last Updated:2025/03/01 Views:

When using watch in Vue , you may need to monitor an object, but only care about certain properties in the object, not all properties of the object. In this case, you can use deep monitoring and calculated properties, or add some options to watch to exclude monitoring of certain properties.


1. Deep listening and calculated properties

Deep monitoring is a feature in Vue.js that monitors changes in object properties and recursively observes each property inside the object. You can use computed properties to filter out properties that do not need to be monitored, so that only the required operations are triggered.

Here is an example:

HTML Code

<div id="app">
  <span>{{obj.a}}</span>
  <button @click="update">update</button>
</div>

JS Code

new Vue({
  el: '#app',
  data: {
    obj: {a: 1, b: 2},
    c: 3
  },
  computed: {
    watchedObj() {
      return {a: this.obj.a};
    }
  },
  methods: {
    update() {
      this.obj.a++;
      this.obj.b++;
      this.c++;
    }
  },
  watch: {
    watchedObj: {
      handler(val, oldVal) {
        console.log('old:', oldVal);
        console.log('new:', val);
      },
      deep: true
    }
  }
});

In this example, we define an object obj and a computed property watchedObj, which contains only the properties we want to monitor. Then we monitor watchedObj in watch and set the deep option to true, so that watchedObj will be triggered only when obj.a changes.


2. Option Filter

In watch , you can use an options object to set some options, including immediate, deep, handler, etc. Another option is watch filtering, which can be used to exclude monitoring of certain attributes.

Here is an example:

HTML Code

<div id="app">
  <span>{{obj.a}}</span>
  <button @click="update">update</button>
</div>

JS Code

new Vue({
  el: '#app',
  data: {
    obj: {a: 1, b: 2},
    c: 3
  },
  methods: {
    update() {
      this.obj.a++;
      this.obj.b++;
      this.c++;
    }
  },
  watch: {
    obj: {
      handler(val, oldVal) {
        console.log('old:', oldVal);
        console.log('new:', val);
      },
      deep: true,
      // Here are the filter options
      watch(prop, val) {
        return prop === 'a';
      }
    }
  }
});

In this example, we use a new option watch in watchobj.a , whose value is a filter function. This function can receive two parameters: prop and val, which represent the property name and new value of the object respectively. In this example, we only monitor the changes of because the filter function only returns obj.a.


Summarize

When using watch in Vue , you can use deep monitoring and calculated properties, or add a filter function in watch to exclude monitoring of certain properties. Either method can help you monitor object properties more effectively.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Display element or text on mouse hover in vue

Publish Date:2025/03/01 Views:177 Category:Vue

Vue.js is a popular JavaScript framework that makes web application development easier and more efficient. In this tutorial, we will learn how to use Vue.js to display an element or text on mouse hover. This tutorial will cover the following

What is the use of the immediate property of watch in Vue?

Publish Date:2025/03/01 Views:65 Category:Vue

In Vue, watch is a way to perform asynchronous tasks or trigger responsive dependencies when data changes. In most cases, watch will be delayed by default. This means that the watch will only be triggered when the value being monitored chang

Setting up checkbox functionality in Vue

Publish Date:2025/03/01 Views:185 Category:Vue

In Vue, checkboxes are a very common interactive component that allows users to select multiple options. This article will introduce how to set up checkbox functionality in Vue and provide some practical examples.

What happens if a child component changes the data in props in Vue?

Publish Date:2025/03/01 Views:138 Category:Vue

In Vue, when a child component changes the data in props, it will cause the responsiveness of the parent component and other child components to change. First, you need to understand that props is a way to pass data from a parent component t

How to refresh the page in Vue

Publish Date:2025/03/01 Views:85 Category:Vue

Vue is a popular JavaScript framework that provides many convenient tools and methods for building web applications. In Vue, page updates are usually achieved through data binding and responsive systems. But sometimes you need to refresh the

How to find all elements by class name in Vue

Publish Date:2025/03/01 Views:182 Category:Vue

Vue is a very powerful JavaScript framework that provides developers with a lot of convenient features and tools. One of them is finding all elements by class name. In this article, we will explore how to find all elements by class name in

How to get the initial state of a certain data in Vue?

Publish Date:2025/03/01 Views:146 Category:Vue

In Vue, sometimes we want to get the initial state of a data in data so that we can compare or reset it in subsequent operations. In this article, we will introduce how to get the initial state of a data in data and provide an example to il

Implementation of scheduling execution in Vue project

Publish Date:2025/03/01 Views:128 Category:Vue

In Vue projects, scheduling execution refers to assigning tasks to different components or instances to implement functions such as data processing, rendering, and interaction. This article will introduce the implementation method of schedul

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial