Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
779 views
in Technique[技术] by (71.8m points)

vuetify.js - v-select not properly synced with vuex store object and computed property

I'm using a <v-select> tag like this:

In the component <template>, I have:

<v-select
  :value="bar"
  @change="update('foo.bar', $event)"
  :items="myItems"
  label="Bar"
/>

In the component <script>, I have:

export default {
  computed: {
    bar() {
      return this.user.foo?.bar
    },
  }
  methods: {
    update(path, value) {
      this.$store.dispatch('commitUser', { path, value })
    }
  }
}

In my store, I have:

export const state = () => ({
  user: {}
}

export const mutations = {
  USER_UPDATE(state, payload) {
    let levels = payload.path.split('.')
    const key = levels.pop()
    const propToChange = levels.reduce((res, level) => {
      if (!res[level]) this._vm.$set(res, level, {})
      return res[level]
    }, state.user)
    this._vm.$set(propToChange, key, payload.value)
  },
}

export const actions = {
  async commitUser({ commit }, payload) {
    commit('USER_UPDATE', payload)
  }
}

This code work pretty fine, however, when commit('USER_UPDATE', payload) is commented, then the store is not updated but the value shown in the select is not synced back to the store value.

How should be handled such issue ?

question from:https://stackoverflow.com/questions/65939741/v-select-not-properly-synced-with-vuex-store-object-and-computed-property

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...