computed计算属性和watch的区别:

计算 ‘ 单价 x 数量 = 总价 ’

watch:就不写了,没意思

computed:
computed:{
        allPrice:function(){
            return this.price*this.count
        }
    }
  computed:{
        allPrice:{
            //get只读
            get:function(){
                return this.price*this.count
            },
            //当改变结果allPrice时我们也可以通过set属性里的一些js逻辑来改变参数的值
            set:function(newVal){
                this.price = newVal/this.count
            }
        }
    }
 

只要改变能影响allPrice值的参数,都会立即改变allPrice页面的值,

watch的话需要监听每一个可能会被改变的值,参数过多时需要写很多同样的计算方法,当然可以调用同一个,但没必要

原文地址:https://www.cnblogs.com/wd163/p/13740535.html