我正在像这样的对象中引用VueX吸气剂:
computed : {
...mapGetters(['activeLayer']),
}
商店获取者如下:
getters : {
activeLayer : state => state.views[state.activeView].layers[state.views[state.activeView].activeLayer]
}
然后我要用手表监视变化:
created {
var that = this;
this.$store.watch( function(state) {return state.views[state.activeView].activeLayer},
function() {
console.log(that.activeLayer); // Returns initial value
that.$store.state.views[this.$store.state.activeView].layers[this.$store.state.views[this.$store.state.activeView].activeLayer]; // Returns correct value
}
,{ deep: true } )
}
问题在于,商店更改activeLayer
时不会更新为新值。
如何强制activeLayer
更新?
答案 0 :(得分:0)
尝试改用mapState
,并注意更改:
import { mapState } from 'vuex';
computed: { ...mapState(['activeLayer']), }
watch: {
activeLayer(newValue, oldValue) {
console.log(newValue);
}
},