2017-03-10 340 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; 
    } 
    } 
});