How to define data examples in Vue

How to define data examples in Vue

Preface

In the development process, defining variables is a very frequent and basic task. How to reasonably define variables according to the usage scenarios and scope of the variables is a very small and easy thing to make mistakes.

Vue2 has been popular for many years. Most developers like to define many variables in the data option during the development process. This is very detrimental to the readability, maintainability and performance of the code. If you want to use variables well, you need to combine the characteristics of Vue and JS.

In Vue, variables can be divided into two types according to whether two-way data binding is required:

One is to hijack the data of Vue and respond to the changes of data to the view in real time.

As long as the data can only change the msg, the msg bound in the template will respond in real time

<template>
  <div>{{msg}}</div>
</template>

<script>
export default {
  data() {
    msg: "" 
  }
};
</script>

There is another method that does not need to be hijacked by Vue data:

Only works in scripts, not used in templates, no data hijacking required

name is only valid in the concatName function, so just define it as a local variable

age is needed in both getAge and concatName functions. It is not appropriate to use it as a local variable. Then its scope can be increased to facilitate its use in multiple places.

<script>
const age = 'bar'
export default {
  methods: {
    getAge() {
      return age
    },
    concatName() {
      let name = 'nordon'
      return `name:${name}, age: ${age}`
    }
  },
};
</script>

It is only used as rendering data in the template. After customization, it will not be modified in subsequent operations. If Vue is used to hijack this data, some performance will be wasted.

<template>
  <div v-for="item in arr">{{item.name}}</div>
</template>

<script>
const arr = Object.freeze([{
  name: 'nordon',
  age: 18
}])
export default {
  data() {
    return {
      arr
    }
  }
};
</script>

Use Object.freeze to freeze the data that does not need data hijacking. When recursively traversing the data in Vue for data hijacking, the data will not be hijacked. Especially for a large number of table-like data, the performance improvement will be significant.

You can see from the Vue source code why after using Object.freeze to process the data, there will be no data hijacking.

function defineReactive (obj, key) {
  // Delete irrelevant code and keep only the judgment condition const property = Object.getOwnPropertyDescriptor(obj, key)
  if (property && property.configurable === false) {
    return
  }
}

Summarize

This is the end of this article about how to define data in Vue. For more relevant Vue definition data content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

<<:  IDEA2020.1.2 Detailed tutorial on creating a web project and configuring Tomcat

>>:  MySQL merges multiple rows of data based on the group_concat() function

Recommend

A brief discussion on HTML table tags

Mainly discuss its structure and some important pr...

Two problems encountered when deploying rabbitmq with Docker

1. Background The following two problems are enco...

How to separate static and dynamic state by combining Apache with Tomcat

Experimental environment Apache and Tomcat are bo...

MySQL UNION operator basic knowledge points

MySQL UNION Operator This tutorial introduces the...

CSS multi-column layout solution

1. Fixed width + adaptive Expected effect: fixed ...

Apply provide and inject to refresh Vue page method

Table of contents Method 1: Call the function dir...

Solution to the garbled problem of web pages when the encoding is set to utf-8

Recently, when I was writing web pages with PHP, I...

Linux swap partition (detailed explanation)

Table of contents linux 1. What is SWAP 2. What d...

Implementation of installing Docker in win10 environment

1. Enter the Docker official website First, go to...

Analyze Mysql transactions and data consistency processing issues

This article analyzes the consistency processing ...

CSS realizes div completely centered without setting height

Require The div under the body is vertically cent...

About the processing of adaptive layout (using float and margin negative margin)

Adaptive layout is becoming more and more common i...

Detailed explanation of some settings for Table adaptation and overflow

1. Two properties of table reset: ①border-collaps...

How to deploy HTTPS for free on Tencent Cloud

Recently, when I was writing a WeChat applet, the...