【问题标题】:Managing State for Overlay Dismissed Components in Vuetify在 Vuetify 中管理 Overlay Dismissed 组件的状态
【发布时间】:2018-05-06 15:49:35
【问题描述】:

我第一次构建了一个vuetify/nuxt 前端,我已经将我的v-navigation-drawer 组件从default.vue 布局中移到了它自己的组件中,这样它就可以在多个方面重复使用布局。

这个抽屉的激活器仍然保留在default.vue 组件中,所以我在vuex 中添加了sidebar 状态:

export const state = () => ({
    baseurl: 'http://example.com/api/',
    sidebar: false,
    authenticated: false,
    token: null,
    user: null,
})

侧边栏的 mutator 如下所示:

export const mutations = {
    toggleSidebar(state) {
        state.sidebar = !state.sidebar;
    }
}

这在打开抽屉时效果很好,但是因为抽屉是通过单击覆盖或单击侧边栏关闭(如果您已关闭覆盖)而关闭的,vuex 会引发一个巨大的错误:

我怎样才能通过 vuex 使其正常工作?

【问题讨论】:

    标签: vuex nuxt.js vuetify.js


    【解决方案1】:

    不要将抽屉的模型直接绑定到$store.state.sidebar,而是在抽屉组件中使用计算设置器。请注意,您必须从抽屉本身传递新值,不要只是切换商店中已有的东西。

    <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

    另一种选择是分别绑定道具和事件

    <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

    【讨论】:

    • 朋友,你真是个天才!值得称赞的是,我非常接近这一点……我只是没有将值传递给突变。但最后在尝试了一切之后......宾果游戏!非常感谢。
    • 你是天才。这就是我一直在寻找的。谢谢!
    • 太棒了! Vuetify v-navigation-drawer 的缺失指南! Vuetify 上有关于此的未解决问题....而答案 - 在大多数情况下 - 是你的!
    • 这是许多未答复帖子的答案。谁能解释val 来自哪里以及它是如何设置的?它只是使用 v-model 生成的默认值吗?
    【解决方案2】:

    另一个解决方案是使用vuex-map-fields 包,它可以为保存在 Vuex 存储中的状态启用双向数据绑定。

    它使代码比常规方式更清晰、更易读(如在接受的答案中)。

    基本示例:

    在您的商店文件中

    
    // Import the `getField` getter and the `updateField`
    // mutation function from the `vuex-map-fields` module.
    import { getField, updateField } from 'vuex-map-fields';
    
    export const state = () => ({
        baseurl: 'http://example.com/api/',
        sidebar: false,
        authenticated: false,
        token: null,
        user: null,
    })
    
    export const getters = {
       // Add the `getField` getter to the
       // `getters` of your Vuex store instance.
       getField,
    }
    
    export const mutations = {
       // Add the `updateField` mutation to the
       // `mutations` of your Vuex store instance.
       updateField,
    }
    
    

    在您的组件中

    template>
      <v-navigation-drawer v-model="sidebar" ...>
    </template>
    
    <script>
    import { mapFields } from 'vuex-map-fields';
    
    export default {
      computed: {
        // The `mapFields` function takes an array of
        // field names and generates corresponding
        // computed properties with getter and setter
        // functions for accessing the Vuex store.
        ...mapFields([
          'baseurl',
          'sidebar',
          // etc...
        ]),
        }
      }
    </script>
    

    更多详情可以查看githab page

    【讨论】:

      猜你喜欢
      • 2019-12-24
      • 2021-05-17
      • 2021-05-09
      • 1970-01-01
      • 2020-07-30
      • 2017-09-16
      • 2017-04-20
      • 2019-07-29
      • 2022-10-24
      相关资源
      最近更新 更多