How can I clone data from Vuex state to local data?

后端 未结 3 1915
别那么骄傲
别那么骄傲 2021-01-05 16:42

How can I clone data from vuex state to local data attribute?

State

this.tsStore.shemes

Data Attribute



        
3条回答
  •  耶瑟儿~
    2021-01-05 17:07

    data(){
      return {
        shemes: null,
      }
    },
    beforeMount() {
      this.shemes = this.stateShemes
    },
    computed: {
      stateShemes() { return this.tsState.shemes }
      // OR that's how i do
      stateShemes() { return this.$store.getters['shemes'] }
    }
    

    UPDATE

    So you get some value from your state by using computed variables. You cannot just assign the value from you store in the data() block. So you should do it beforeMount. That way if you have a watcher for shemes variable, it won't trigger on assigning computed value. If you put it in mounted() hook, the watcher will trigger.

    Also, can you explain why do you use this call this.tsState.shemes instead of this.$store.getters.shemes?

提交回复
热议问题