本文介绍了Vue 2.0 - 计算问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当点击启动方法增量"变量点击"改变但为什么它不改变计数器".应该是因为我在计数器函数中引用了clicks"变量.

When on click start method "increment" variable "clicks" change but why it not change "counter". It should be because i have in counter function reference to "clicks" variable.

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/

推荐答案

不要在数据上定义计数器.计算出的值就像一个数据属性.

Don't define counter on your data. The computed value acts like a data property.

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;
    }
  }
});

这篇关于Vue 2.0 - 计算问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:29