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

Configuring Apache Web Server on Ubuntu and Debian

Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM

This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

My understanding of webservice is this

Publish Date:2025/03/18 Views:150 Category:NETWORK

Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat

Which technology do you choose to implement the web chat room?

Publish Date:2025/03/18 Views:62 Category:NETWORK

With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio

How to redirect a website from HTTP to HTTPS

Publish Date:2025/03/16 Views:117 Category:NETWORK

HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi

How to avoid soft 404 in the website during SEO

Publish Date:2025/03/17 Views:136 Category:NETWORK

This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand

Webpack packages ES6 and CommonJs mixed React

Publish Date:2025/03/02 Views:179 Category:React

This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial