2017-03-10 127 views
1

當點擊start方法「增量」變「點擊」的變化,但爲什麼它不會改變「計數器」。應該是因爲我在計數器函數中引用了「clicks」變量。Vue公司2.0 - 計算問題

new Vue({ 
    el: '#app', 
    data: { 
    title: 'helloworld', 
    cssClass: '', 
    clicks: 0, 
    counter: 0 
    }, 
    methods: { 
    changeTitle() { 
     this.title = 'helloworld new'; 
    }, 
    increment() { 
     this.clicks++; 
    } 
    }, 
    computed: { 
    counter() { 
     return this.clicks * 2; 
    } 
    } 
}); 

https://jsfiddle.net/freeq343/b7fyeyxm/

回答

1

不要在你的數據定義計數器。計算的值就像一個數據屬性。

new Vue({ 
    el: '#app', 
    data: { 
    title: 'helloworld', 
    cssClass: '', 
    clicks: 0 
    }, 
    methods: { 
    changeTitle() { 
     this.title = 'helloworld new'; 
    }, 
    increment() { 
     this.clicks++; 
    } 
    }, 
    computed: { 
    counter() { 
     return this.clicks * 2; 
    } 
    } 
});