Vuex参考未返回正确值

时间:2020-04-08 18:18:20

标签: vue.js vuex

我正在像这样的对象中引用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更新?

1 个答案:

答案 0 :(得分:0)

尝试改用mapState,并注意更改:

import { mapState } from 'vuex';
computed: { ...mapState(['activeLayer']), }
watch: {
     activeLayer(newValue, oldValue) {
          console.log(newValue);
     }
},