官术网_书友最值得收藏!

  • Vue.js Quick Start Guide
  • Ajdin Imsirovic
  • 263字
  • 2021-06-24 18:23:56

Computed properties and watchers

Computed properties are used to avoid complex logic adding bloat to your views. In other words, computed properties are useful to hide the complexity from our HTML and thus keep our HTML understandable, easy to use, and declarative. Put differently, when we need to compute some values from the data option, we can do that with the help of computed properties.

The full code for the following example can be seen at https://codepen.io/AjdinImsirovic/pen/WyXEOz:

<!-- HTML -->
<div id="example">
<p>User name: "{{ message }}"</p>
<p>Message prefixed with a title: "{{ prefixedMessage }}"</p>
</div>

// JS
var example = new Vue({
el: '#example',
data: {
userName: 'John Doe',
title: ''
},
computed: {
// a computed getter
prefixedMessage: function () {
// `this` points to the Vue instance's data option
return this.title + " " + this.userName
}
}
})
Computed properties are cached. As long as a computed property's dependencies do not change, Vue will return the cached value of the computed property.

Watchers are not as frequently used as computed properties are. In other words, the watch option is to be used less frequently than the computed properties option. Watchers are commonly used for asynchronous or otherwise costly operations with changing data.

Watchers have to do with reactive programming; they allow us to observe a sequence of events through time and react to changes as they happen on a certain data property. 

We will cover the subject of computed properties and watchers in later chapters. For now, it is sufficient to know that they exist in Vue and that they are widely used.

主站蜘蛛池模板: 丰顺县| 贡觉县| 镇赉县| 宝坻区| 海淀区| 台北县| 息烽县| 禹城市| 乐亭县| 上饶县| 仪征市| 唐河县| 新河县| 顺昌县| 元朗区| 呼玛县| 中牟县| 萨嘎县| 太仆寺旗| 巩义市| 崇阳县| 双柏县| 博客| 徐汇区| 南丰县| 四川省| 登封市| 思茅市| 灌阳县| 津市市| 会同县| 贵阳市| 黔西| 双鸭山市| 靖宇县| 西丰县| 上饶县| 财经| 浪卡子县| 疏勒县| 资阳市|