Use of computed attribute Vue

Vue computed property is called computed attribute. In this section, we learn how to calculate the property Vue uses? I remember when learning the knowledge of the template associated Vue, know that you can use in the template expressions , and expressions within the template is very convenient, but this traversal is a certain limit, they are actually used in some simple operation. That is, if put too much logic in the template template will be too heavy and difficult to maintain. Let's look at an example:

<p id="app">

  <h1>{{ message.split('').reverse().join('') }}</h1>

</p>

 

In this example, the template is no longer simple and clear. You have to look at some time to realize that, here is the flip want to display the variable message string . When you want to refer to flip the string here many times in the template, it will be more difficult to handle.

This is the reason for any complex logic, you should use the calculation attribute. Next we take a calculated property Vue in learning.

Calculating attribute property can be used to quickly calculate the display view (View) in. These calculations will be cached, and only updated when necessary.

There are a variety of methods in a view Vue settings:

  • Use instructions directly bind the data value to the view

  • Using simple expressions for content simple conversion

     

  • Use filters the content simple conversion

     

In addition, we can also show calculated value based on the data model or a set of values ​​calculated using the attribute.

Computed Property

Calculate property allows us to calculate the specified view, complex values. These values ​​will be bound to a dependency value, only updated when necessary.

For example, we can have an array of results in the data model:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

data () {

  return {

    results: [

      {

        name: 'English',

        marks: 70

      },

      {

        name: 'Math',

        marks: 80

      },

      {

        name: 'History',

        marks: 90

      }

    ]

  }

}

Suppose we want to see the total number of topics. We can not use filters or expressions to accomplish this task.

  • filters: a simple data format, in a plurality of positions that require the application

  • expressions: stream operation or other complex logic is not allowed. They should keep it simple

     

This time, calculate properties can come in handy. We can add value to a calculation model, as follows:

1

2

3

4

5

6

7

8

9

10

computed: {

  totalMarks: function () {

    let total = 0

    let me = this

    for (let i = 0; i < me.results.length; i++) {

      total += parseInt(me.results[i].marks)

    }

    return total

  }

}

totalMarks 计算属笥使用数组 resultes 的 marks 计算出总值。它只是循环遍历值并返回子总数。

然后,我们可以在视图中显示计算值:

1

2

3

4

5

6

7

8

9

<p id="app">

  <p v-for="subject in results">

    <input v-model="subject.marks">

    <span>Marks for {{ subject.name }}: {{ subject.marks }}</span>

  </p>

  <p>

    Total marks are: {{ totalMarks }}

  </p>

</p>

计算属性 vs 方法

我们可以使用Vue中的 method 计算出学科的总分,最终得到的总数结果是相同的。

在上例的基础上,我们把 computed 区块中的 totalMarks 函数整体移到 methods 中。同时在模板中将 {{ totalMarks }} 替换成 {{ totalMarks() }} 。 你最终看到的结果是一样的,如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

let app = new Vue({

 el: '#app',

 data () {

  return {

   results: [

    {

     name: '英语',

     marks: 70

    },

    {

     name: '数学',

     marks: 80

    },

    {

     name: '历史',

     marks: 90

    }

   ]

  }

 },

 methods: {

  totalMarks: function () {

   let total = 0

   let me = this

   for (let i = 0; i < me.results.length; i++) {

    total += parseInt(me.results[i].marks)

   }

   return total

  }

 }

})

虽然这两种方式输出的结果是相同的,但是性能将遭受毁灭性的打击。使用这种方法, totalMarks() 方法在每次页面渲染时都被执行一次(例如,使用每一个 change )。

如果我们有一个计算属性,那么Vue会记住计算的属性所依赖的值(在我们这个示例中,那就是 results )。通过这样做,Vue只有在依赖变化时才可以计算值。否则,将返回以前缓存的值。这也意味着 只要 results 还没有发生改变,多次访问 totalMarks 计算属性会立即返回之前的计算结果,而不必再次执行函数。

上面两个示例也说明,在Vue中 计算属性是基于它们的依赖进行缓存的,而方法是不会基于它们的依赖进行缓存的。从而使用计算属性要比方法性能更好。

这也同样意味着下面的计算属性将不再更新,因为 Date.now() 不是响应式依赖:

1

2

3

4

5

computed: {

  now: function () {

    return Date.now()

  }

}

相比之下,每当触发重新渲染时,方法的调用方式将总是再次执行函数。因此,函数必须是一个纯函数。它不能有副作用。输出只能依赖于传递给函数的值。

那么我们为什么需要缓存?假设我们有一个性能开销比较大的的计算属性 A ,它需要遍历一个极大的数组和做大量的计算。然后我们可能有其他的计算属性依赖于 A 。如果没有缓存,我们将不可避免的多次执行 A 的 getter !如果你不希望有缓存,请用方法来替代。

计算属性的 setter

计算属性默认只有 getter ,不过在需要时你也可以提供一个 setter :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

computed: {

  fullName: {

    // getter

    get: function () {

      return this.firstName + ' ' + this.lastName

    },

    // setter

    set: function (newValue) {

      var names = newValue.split(' ')

      this.firstName = names[0]

      this.lastName = names[names.length - 1]

    }

  }

}

 

你在输入框中输入一个 fullName ,然后点击 set 按钮,可以看到对应的效果。你现在再运行 app.fullName="Airen liao" 时,计算属性的 setter 会被调用, app.firstName 和 app.lastName 也相应地会被更新。

 

观察者

虽然计算属性在大多数情况下更合适,但有时候也需要一个自定义的 watcher 。这是为什么Vue通过 watch 选项提供一个更通用的方法,来响应数据的变化。当你想要在数据变化响应时,执行异步操作或开销较大的操作,这是很有用的。

Vue确实提供了一种更通用的方式来观察和响应Vue实例上的数据变动: watch 属性 。当你有一些数据需要随着其它数据变动而变动时,你很容易滥用 watch 。然而,通常更好的想法是使用计算属性而不是命令式的 watch 回调。比如下面的示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<p id="app">

  {{ fullName }}

</p>

let app = new Vue({

  el: '#app',

  data () {

    return {

      firstName: 'Foo',

      lastName: 'Bar',

      fullName: 'Foo Bar'

    }

  },

  watch: {

    firstName: function (val) {

      this.fullName = val + ' ' + this.lastName

    },

    lastName: function (val) {

      this.fullName = this.firstName + ' ' + val

    }

  }

})

上面代码是命令式的和重复的。将它与计算属性的版本进行比较:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

let app = new Vue({

  el: '#app',

  data () {

    return {

      firstName: 'Foo',

      lastName: 'Bar'

    }

  },

  computed: {

    fullName: function () {

      return this.firstName + ' ' + this.lastName

    }

  }

})

在Vue中使用异步计算属性

Vue中的计算属性非常好。它们允许你执行复杂的操作或数据格式,同时最大限度地执行依赖项计算的性能,只在依赖更改时更新视图。但遗憾的是,它们完全是同步的。

值得庆幸的是,有一个插件。使用vue-async-computed 包可以通地将一个 promise 的值绑定到组件属性来创建和使用组件中的异步计算属性。

我们可以在项目的根目录下通过 yarn 或 npm 来安装 vue-async-computed 插件:

1

2

3

4

# Yarn

$ yarn add vue-async-computed

# NPM

$ npm i vue-async-computed --save

接下来在你的项目中开启这个插件:

1

2

3

4

5

6

7

8

9

// main.js

import Vue from 'vue';

import AsyncComputed from 'vue-async-computed'

import App from 'App.vue';

Vue.use(AsyncComputed);

new Vue({

  el: '#app',

  render: h => h(App)

});

如果你和我一样,对Vue的构建工具不是很熟悉的话,我建议你使用Vue官方提供的构建工具 Vue CLI 。默认情况,它提供了五种模板,你可以根据自己喜欢的方式选择自己需要的模板即可。

确认在项目中引用 vue-async-computed 之后,咱们就可以开始使用这个插件了。使用如何使用这个插件之前,先来简单的了解一些概念。

在Vue中标准计算属性和异步属性之间有一些区别:

  • 异步属性不能有 setter

  • 直到 promise 的 resolve 为止,除非 default 被设置,否则该值为 null

     

在大多数情况下,你可以将它们视为返回 promise 的计算属性。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<!-- MyComponent.vue -->

<template>

  <!-- 在一两秒后 myResolvedValue将变成"*Fancy* Resolved Value" -->

  <h2>Asynchronous Property {{ myResolvedValue }}</h2>

</template>

<script>

  export default {

    asyncComputed: {

      myResolvedValue () {

        return new Promise((resolve, reject) => {

          setTimeout(() => resolve('*Fancy* Resolved Value!'), 1000)

        })

      }

    }

  }

</script>

使用ES7 / ES2016的 async / await ,这将变得更简单:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<!-- MyComponent.vue -->

<template>

  <!-- 在一两秒后 myResolvedValue将变成"*Fancy* Resolved Value" -->

  <h2>Asynchronous Property {{ myResolvedValue }}</h2>

</template>

<script>

  function fancinessComesLater () {

    return new Promise((resolve, reject) => {

      setTimeout(() => resolve('*Fancy* Resolved Value!'), 1000)

    })

  }

  export default {

    asyncComputed: {

      async myResolvedValue() {

        return await fancinessComesLater()

      }

    }

  }

</script>

有关于vue-async-computed 更详细的使用和介绍,可以阅读其 官网 提供的相关介绍。

总结

今天主要学习了Vue中的计算属性。在Vue中的计算属性可以让我们很好的监听多个数据或者一个数据来维护返回一个状态值,只要其中一个或多个数据发生变化,则会重新计算整个函数体,重新皇家马德里回状态值,从而更新对应的视图(View)。其次,计算属性具有缓存,相比Vue中的方法而言,性能更佳。但Vue中的计算属性都是同步的,如果需要异步我们得依赖于vue-async-computed 。

由于自己是Vue的初学者,对Vue的计算属性也只是停留在表面层上的理解,如果从深层面上看,还是会存在一定问题。希望各咱大婶能指正或提供自己的经验。

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

 

Guess you like

Origin www.cnblogs.com/shashazhu/p/11611298.html