Home > Web Front-end > Vue.js > body text

How to use Vue.set to implement responsive data in Vue

PHPz
Release: 2023-06-11 11:28:40
Original
2083 people have browsed it

Vue provides a very convenient way to update responsive data, which is to use the Vue.set method. This article will introduce how to use the Vue.set method and its related knowledge points.

1. Introduction to Vue.set method

The Vue.set method is a global API provided by Vue, which is used to add a property to a responsive object and ensure that the new property is responsive. of. In Vue, we usually use objects and arrays as data sources for components. These data sources are usually observed and support monitoring by Vue's responsive system. However, in some cases we need to add a new attribute or element to the object or array. At this time, if the Vue.set method is not used, the added new attribute or element will not be monitored by the responsive system, resulting in some problems.

2. Use of Vue.set method

The Vue.set method is a global API, so we can use it directly in the component. The following is how to call the Vue.set method:

Vue.set(obj, key, value)

Among them, obj represents the object to be operated on, key represents the attribute name to be added, and value Indicates the attribute value to be added.

We can demonstrate the use of Vue.set through the following code:

<template>
  <div>
    <p>{{ userInfo.name }}</p>
    <p>{{ userInfo.age }}</p>
    <button @click="addData">添加数据</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userInfo: {
        name: '张三',
        age: 18
      }
    }
  },
  methods: {
    addData() {
      Vue.set(this.userInfo, 'phone', '123456789') // 添加响应式属性
    }
  }
}
</script>
Copy after login

In the above code, we added a new attribute phone to the userInfo object and used Vue.set Ways to make sure it's responsive. When we click the button, an attribute named phone with a value of '123456789' will be added to the userInfo object. We can view the result in the template. At this time, the add operation using Vue.set can not only get the response in subsequent updates, it can also trigger any data binding updates for the object.

3. Principle analysis of Vue.set

The implementation principle of Vue.set is not complicated and is mainly implemented by calling Vue’s responsive system. For the object addition operation, the implementation of Vue.set is as follows:

Vue.set = function (obj, key, value) {
  // 判断obj是否是响应式的对象
  if (hasOwn(obj, key)) {
    obj[key] = value
    return
  }
  // 获取当前的观察者
  const ob = obj.__ob__
  // 非响应式对象,直接赋值
  if (!ob) {
    obj[key] = value
    return
  }
  // 将新增属性的值变为响应式的
  defineReactive(ob.value, key, value)
  ob.dep.notify()
}
Copy after login

First, the Vue.set method will determine whether obj is a responsive object through Vue's hasOwn method. If so, assign a value to the object directly. If not, it will get obj's observer ob, then call the defineReactive method to make the new property responsive, and call the ob.dep.notify method to notify the component to update.

For the addition operation of the array, the implementation of Vue.set is as follows:

Vue.set = function (target, key, val) {
  if (Array.isArray(target) && isValidArrayIndex(key)) {
    target.length = Math.max(target.length, key)
    target.splice(key, 1, val)
    return val
  }
  if (hasOwn(target, key)) {
    target[key] = val
    return val
  }
  const ob = target.__ob__
  if (!ob) {
    target[key] = val
    return val
  }
  ob.convert(key, val)
  ob.dep.notify()
  return val
}
Copy after login

We can see that for the addition operation of the array, Vue.set is mainly implemented using the splice method. At the same time, for array operations, we can also use Vue.set to ensure that the added elements are responsive.

4. Summary

This article mainly introduces the usage and implementation principle of the Vue.set method. The Vue.set method is a very convenient way provided by Vue to implement responsive data. renew. Whether adding elements to an object or an array, we can use the Vue.set method to ensure that these new elements are responsive, thereby solving some responsive data update problems.

The above is the detailed content of How to use Vue.set to implement responsive data in Vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!