有时候我们在运行我们的项目的时候会出现如下警告
原因:一个计算属性,如果没有设置 setter,也就是传入的是一个函数,或者传入的对象里没有 set 属性,当你尝试直接改变这个计算属性的值,都会报这个错误,计算属性默认只有getter。
解决:把computed中的route属性改成如下格式
computed: { route () { // getter get: function () { return this.$store.state.curTab.route }, // setter set: function (newValue) { this.$store.state.curTab.route = newValue } }
computed VS watch
computed比较适合的场景是多个数据影响一个数据,比如商品总价随着商品数量及商品种类的影响
watch比较适合的场景是一个数据影响多个数据,比如监控路由、input输入框的值特殊处理等等