Introduction to Computed Properties in Vue

Introduction to Computed Properties in Vue

1. What is a calculated property?

Expressions in templates are very convenient, but they are designed primarily for simple calculations. Putting too much logic in a template can make it cumbersome and difficult to maintain.

For example:

<div id="app">
  {
<!-- -->{ message.split('').reverse().join('') }}
</div>


At this point, the template is no longer just a simple declarative logic. Instead, directly reverse the string in the interpolation expression. If you use the reversed string in multiple places, it will be troublesome to write it this way and increase the consumption. So, for any complex logic, you should use computed properties.

2. Syntax of computed properties

computed{

function () {return //Must return a value. }Usually this function is a get function}

3. Examples

For the above example, we can write:

<div id="app">
        <p>Original string: {
<!-- -->{mes}}</p>
        <p>Reversed string: {
<!-- -->{reverseMes}}</p>
    </div>
let vm = new Vue({
            el:'#app',
            data:{
                mes:'sayhello'
            },
            computed: {
                reverseMes(){
        // The calculated attribute must have a return value return this.mes.split('').reverse().join('')
                }
            }
        })


View the results:

Here we define a function in computed property of the vue instance. The return value of the function is the result we need, which can be called directly in the interpolation expression and rendered.

For example, by calculating the property, the first letter of the word is capitalized:

<div id="app">
        <p>Original string: {
<!-- -->{name}}</p>
        <p>Capitalize the first letter: {
<!-- -->{toUpperCase}}</p>
    </div>


In the computed property of the Vue instance, customize our calculated property by getting the first character of the string and converting it to uppercase, and then concatenating it with the remaining characters after the split:

el:"#app",
            data:{
                name:'tom'
            },

            // Computed properties computed:{
            // Custom calculated property toUpperCase(){
                return this.name.charAt(0).toUpperCase().concat(this.name.slice(1,3))
              }
            }

The output is:

There are two very practical tips for calculated properties that are easily overlooked: one is that calculated properties can depend on other calculated properties; the other is that calculated properties can depend not only on the data of the current Vue instance, but also on the data of other instances.

For example:

<div id="app1"></div>

    <div id="app2">
        {
<!-- -->{reverseMes}}
    </div>
let vm1 = new Vue({
            el:'#app1',
            data:{
                mes:'hello'
            }
        })

        let vm2 = new Vue({
            el:'#app2',
            computed: {
                reverseMes(){
                    // Use the mes in the data center of the instance vm1 to reverse the string return vm1.mes.split('').reverse().join('')
                }
            }
        })

View the results:

The data in the instance vm1 and vm2 can also be used to calculate the properties

In addition to being used in interpolation expressions, custom computed properties can also be used in v-bind : attribute binding to perform some style changes, etc.

This is the end of this article about the introduction of calculated properties in Vue. For more relevant Vue calculated properties 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!

You may also be interested in:
  • Vue calculated property implementation transcript
  • A brief talk about calculated properties and property listening in Vue
  • Vue computed properties
  • Vue uses calculated properties to complete the production of dynamic sliders
  • Vue monitoring properties and calculated properties
  • Computed properties and data acquisition methods in Vue
  • Do you know Vue's computed properties?
  • Three implementation methods of Vue's calculated property name case

<<:  Table td picture horizontally and vertically centered code

>>:  Detailed tutorial on installing SonarQube using Docker

Recommend

Examples of using the ES6 spread operator

Table of contents What are spread and rest operat...

Implementation of Nginx hot deployment

Table of contents Semaphore Nginx hot deployment ...

Manually implement js SMS verification code input box

Preface This article records a common SMS verific...

js implementation of verification code case

This article example shares the specific code of ...

How to create a Django project + connect to MySQL

1: django-admin.py startproject project name 2: c...

Example of using JSX to build component Parser development

Table of contents JSX environment construction Se...

Implementation example of JS native double-column shuttle selection box

Table of contents When to use Structural branches...

Linux process management tool supervisor installation and configuration tutorial

Environment: CentOS 7 Official documentation: htt...

A brief analysis of the basic concepts of HTML web pages

What is a web page? The page displayed after the ...

How to use uni-app to display buttons and search boxes in the top navigation bar

Recently, the company is preparing to develop an ...

How to install and configure the Apache Web server

Learn how to host your own website on Apache, a r...

Vue gets token to implement token login sample code

The idea of ​​using token for login verification ...

Vue realizes the whole process of slider drag verification function

Rendering Define the skeleton, write HTML and CSS...