Home Web Front-end Vue.js How to use Vue.set to implement responsive data in Vue

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

Jun 11, 2023 am 11:28 AM
Responsive data vueset

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Methods and examples of dynamically adding attributes using the Vue.set function Methods and examples of dynamically adding attributes using the Vue.set function Jul 24, 2023 pm 07:22 PM

Methods and examples of using the Vue.set function to dynamically add properties. In Vue, if we want to dynamically add a property to an existing object, we usually use the Vue.set function. The Vue.set function is a global method provided by Vue.js, which can ensure responsive updates when adding properties. This article will introduce the use of Vue.set and provide a specific example. First of all, in Vue, we usually use the data option to declare responsive data.

How to use watch in Vue to monitor changes in responsive data How to use watch in Vue to monitor changes in responsive data Jun 11, 2023 am 09:27 AM

With the continuous development of front-end frameworks, Vue has become one of the front-end frameworks that many companies like to adopt, and it is relatively simple. In the process of using Vue to develop applications, responsive data is a feature we often use. In the control of responsive data changes, watch is a very useful feature provided by Vue. In this article, we will introduce in detail how to use watch to monitor changes in responsive data in Vue, as well as some precautions. What is Watchwatch is a kind of Vue

A Beginner's Guide to VUE3: Responsive Data and Computed Properties A Beginner's Guide to VUE3: Responsive Data and Computed Properties Jun 16, 2023 am 11:31 AM

In modern front-end frameworks, reactive data and computed properties are very important concepts. As a Vue3 beginner, after learning these two concepts, you can better understand the features and usage of the Vue framework. This article will focus on explaining Vue3’s responsive data and computed properties, including their basic concepts, usage and examples. If you are not familiar with Vue3 yet, please learn the basics of Vue3 first. 1. What is responsive data? In Vue3, responsive data is a method that can automatically track changes and update the page immediately.

How to use watchEffect in Vue to monitor responsive data and update DOM How to use watchEffect in Vue to monitor responsive data and update DOM Jun 11, 2023 pm 02:55 PM

As a front-end framework, one of the cores of Vue is data-driven views. When the data defined in Vue changes, the corresponding view is also updated. In Vue3, in order to optimize performance and response speed, the reactivity system and CompositionAPI were introduced. As part of a responsive system, watchEffect can monitor data changes and update the DOM instantly. This article will introduce in detail how to use watchEffect to monitor responsiveness in Vue.

Detailed explanation of Vue.set function and how to dynamically add responsive attributes Detailed explanation of Vue.set function and how to dynamically add responsive attributes Jul 24, 2023 pm 11:58 PM

Detailed explanation of Vue.set function and how to dynamically add responsive attributes Vue.js is a popular JavaScript framework that provides a simple, efficient, and flexible way to build user interfaces. In Vue.js, we can use responsive attributes to implement two-way binding of data, so that data changes can be automatically reflected on the interface. However, sometimes we need to add responsive properties dynamically, then we need to use the Vue.set function. The Vue.set function is one provided by Vue.js

How to use Vue.set to implement responsive data in Vue How to use Vue.set to implement responsive data in Vue Jun 11, 2023 am 11:28 AM

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 the 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. In Vue, we usually use objects and arrays as data sources for components. These data sources are usually observed.

Vue responsive data principle and the difference between Vue.set and Vue.$set Vue responsive data principle and the difference between Vue.set and Vue.$set Jun 09, 2023 pm 04:10 PM

Vue is a popular development framework in the current front-end field. One of its core mechanisms that can trigger view updates through data modification is responsive data. This article will delve into the principles of Vue's reactive data and explore the differences between Vue.set and Vue.$set. 1. Vue responsive data principle Vue will first hijack the data source. To define the data source, it only needs to meet the following three conditions: the object is not empty, the object is not a Frozen or Sealed object, and the object is not a Vue instance itself. In due place

Detailed explanation of Vue.observable function and how to use it to create responsive data Detailed explanation of Vue.observable function and how to use it to create responsive data Jul 25, 2023 pm 12:22 PM

Detailed explanation of Vue.observable function and how to use it to create responsive data Introduction: Vue is a popular JavaScript framework that provides powerful tools to build responsive user interfaces. In Vue, responsive data is very important, which allows data changes to be automatically updated to the corresponding view. Reactive data in Vue is usually created by using the data option of the Vue instance. However, in some cases we want to create some independent reactive data objects

See all articles