Managing State for Overlay Dismissed Components in Vuetify

后端 未结 1 829
小蘑菇
小蘑菇 2020-12-16 05:50

I\'m building out a vuetify/nuxt frontend for the first time, and I\'ve moved my v-navigation-drawer component out of the default.vue

相关标签:
1条回答
  • 2020-12-16 06:06

    Instead of binding the drawer's model directly to $store.state.sidebar, use a computed setter in the drawer component. Note that you must pass in the new value from the drawer itself, don't just toggle whatever's already in the store.

    <template>
      <v-navigation-drawer v-model="drawer" ...>
    </template>
    
    <script>
      export default {
        computed: {
          drawer: {
            get () {
              return this.$store.state.sidebar
            },
            set (val) {
              this.$store.commit('sidebar', val)
            }
          }
        }
      }
    </script>
    
    // vuex mutation
    sidebar (state, val) {
      state.sidebar = val
    }
    

    https://vuejs.org/v2/guide/computed.html#Computed-Setter
    https://vuex.vuejs.org/en/forms.html

    Another option is to bind the prop and event separately

    <template>
      <v-navigation-drawer :value="$store.state.sidebar" @input="$store.commit('sidebar', $event)" ...>
    </template>
    

    https://vuejs.org/v2/guide/components.html#Using-v-model-on-Components

    0 讨论(0)
提交回复
热议问题