實例javascript
<div id="example"> <p>Original message: "{{ message }}"</p> <p>Computed reversed message: "{{ reversedMessage }}"</p> </div>
var vm = new Vue({ el: '#example', data: { message: 'Hello' }, computed: { // 計算屬性的 getter reversedMessage: function () { // `this` 指向 vm 實例 return this.message.split('').reverse().join('') } } })
結果: Original message: "Hello" Computed reversed message: "olleH"
<p>Reversed message: "{{ reversedMessage() }}"</p>
// 在組件中 methods: { reversedMessage: function () { return this.message.split('').reverse().join('') } }
computed: { now: function () { return Date.now() } }