Vue resets data to its initial state

Vue resets data to its initial state

In some cases, the data in data needs to be reused, but the data in data has been assigned to various forms, variables, etc., so how to reset the value of data?

1. Assign values ​​one by one

...
data() {
 return {
 name: '',
 sex: '',
 desc: ''
 }
}
...
// Assign values ​​one by one this.name = ''
this.sex = ''
this.desc = ''

This method is clumsy, and of course it can achieve the desired effect, but it is troublesome to reassign values ​​one by one and the code will look messy.

The following method is definitely what you like, one line of code to get it done~

2. Use Object.assign()

MDN introduction to this method: The Object.assign() method is used to copy the values ​​of all enumerable properties from one or more source objects to a target object. It will return the target object.

Usage: Object.assign(target, ...sources)

The first parameter is the target object, and the second parameter is the source object, which means copying the source object properties to the target object and returning the target object.

This is to copy the properties of one object to another object

In Vue:

this.$data gets the data in the current state

this.$options.data() Get the data of the component in its initial state

Therefore, we can copy the data of the initial state to the data of the current state to achieve the reset effect:

Object.assign(this.$data, this.$options.data())

Of course, if you only want to reset a certain object or attribute in data:

this.form = this.$options.data().form

Extensions

The Object.assign(target, ...sources) method can also be used to merge objects:

const o1 = { a: 1 };
const o2 = { b: 2 };
const o3 = { c: 3 };
const obj = Object.assign(o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
console.log(o1); // { a: 1, b: 2, c: 3 }, Note that the target object itself will also change.

If the object contains the same attributes, take the last attribute:

const o1 = { a: 1, b: 1, c: 1 };
const o2 = { b: 2, c: 2 };
const o3 = { c: 3 };
const obj = Object.assign({}, o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 } The property takes the property of the last object

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • How to implement vue data to restore initialization data
  • Vue restores data to its initial state && re-renders the component instance
  • Computed properties and data acquisition methods in Vue
  • How to initialize data in Vue

<<:  How to use squid to build a proxy server for http and https

>>:  MySql forgotten password modification method is suitable for versions 5.7 and above

Recommend

Apache Spark 2.0 jobs take a long time to finish when they are finished

Phenomenon When using Apache Spark 2.x, you may e...

The w3c organization gives style recommendations for html4

This is the style recommendation given by the W3C ...

How to set npm to load packages from multiple package sources at the same time

Table of contents 1. Build local storage 2. Creat...

How to implement checkbox & radio alignment

Not only do different browsers behave differently...

Detailed explanation of transactions and indexes in MySQL database

Table of contents 1. Affairs: Four major characte...

How to implement variable expression selector in Vue

Table of contents Defining the HTML structure Inp...

Four ways to compare JavaScript objects

Table of contents Preface Reference Comparison Ma...

Practice of using Tinymce rich text to customize toolbar buttons in Vue

Table of contents Install tinymce, tinymce ts, ti...

js canvas realizes slider verification

This article example shares the specific code of ...

Web Standard Application: Redesign of Tencent QQ Home Page

Tencent QQ’s homepage has been redesigned, and Web...

How to solve the problem of margin overlap

1. First, you need to know what will trigger the v...

Methods of adaptive web design (good access experience on mobile phones)

1. Add the viewport tag to the HTML header. At th...

How to implement digital paging effect code and steps in CSS

A considerable number of websites use digital pagi...

Summary of common functions and usage methods of WeChat applet development

Here, I have mainly sorted out some commonly used...