简体   繁体   English

VueJS + Vuex + Vuetify 导航抽屉

[英]VueJS + Vuex + Vuetify Navigation Drawer

I have a page with a toolbar and a sidebar.我有一个带有工具栏和侧边栏的页面。 The sidebar is only visible when a user is logged in. The sidebar Navigation Drawer is default not visible if the user is on a mobile device.侧边栏仅在用户登录时可见。如果用户在移动设备上,侧边栏导航抽屉默认不可见。

Now i want a button in the toolbar that the user can open the sidebar.现在我想要工具栏中的一个按钮,用户可以打开侧边栏。 But the toolbar and the sidebar are two different components.但是工具栏和侧边栏是两个不同的组件。 Therefore i'm using Vuex to manage the state.因此我使用 Vuex 来管理状态。 But the state is computet and has no setter, so i can't use the state direct in the navigaion controllers v-model.但是状态是计算的并且没有设置器,所以我不能直接在导航控制器 v 模型中使用状态。

Now i read that you can define a get and a set method on computed variables.现在我读到您可以在计算变量上定义 get 和 set 方法。 But is this the best way to do this?但这是最好的方法吗?

in your template:在您的模板中:


<template>
 <v-navigation-drawer
      :value="isHomeNavigationDrawerOpen"
      @input="TOGGLE_HOME_NAVIGATION_DRAWER"
      ...>
 </v-navigation-drawer>
</template>

<scripts>
import { mapState, mapActions } from 'vuex'
export default {

  computed: {
    ...mapState('userData', ['user', 'loggedIn']),
    ...mapState('toolbar', ['isHomeNavigationDrawerOpen']),
  },
  methods:{
    ...mapActions('toolbar', ['TOGGLE_HOME_NAVIGATION_DRAWER']),
  }
...

In your store module toolbar.js (or your module name)在您的商店模块中toolbar.js(或您的模块名称)


export default {
  state: {
    isHomeNavigationDrawerOpen: null,
  },
  actions: {
    TOGGLE_HOME_NAVIGATION_DRAWER(context, open) {
      context.commit('TOGGLE_HOME_NAVIGATION_DRAWER', open)
    },
  },
  mutations: {
    TOGGLE_HOME_NAVIGATION_DRAWER: (state, open) => {
      state.isHomeNavigationDrawerOpen = open
    },
  },
}

In Vuetify, the component v-navigation-drawer emit a event called 'input' used by v-model.在 Vuetify 中,组件 v-navigation-drawer 发出一个名为“input”的事件,由 v-model 使用。

This event is emitted when the navigation drawer is displayed and when it is closed.当导航抽屉显示和关闭时会发出此事件。 If we call the 'toggle' function in both cases we will enter an infinite loop.如果我们在这两种情况下都调用“toggle”函数,我们将进入无限循环。

I have the same problem and it is what is happening to me.我有同样的问题,这就是发生在我身上的事情。

The way vuex want you to do is to use the proper state mutation. vuex 希望你做的就是使用正确的状态突变。

@click="$store.commit('open-sidebar')"

And the computed value will react to the mutation.并且计算出的值将对突变做出反应。

On your toolbar component , this is where you want your button;在您的toolbar component ,这是您想要按钮的位置; Define a drawer boolean property.定义一个抽屉布尔属性。 The html with the button in the toolbar component would look something like this: toolbar component带有按钮的 html 看起来像这样:

<v-toolbar color="primary" dark app :clipped-left="$vuetify.breakpoint.mdAndUp" fixed>
        <v-toolbar-side-icon @click.stop="$emit('update:drawer', !drawer)"></v-toolbar-side-icon>

In the toolbar component parent you would also want to declare an drawer variable.toolbar component父级中,您还需要声明一个抽屉变量。 Then the html would look something like this:然后 html 看起来像这样:

<toolbar :drawer.sync="drawer"></toolbar>
            <v-navigation-drawer class="secondary" dark fixed :clipped="$vuetify.breakpoint.mdAndUp" app v-model="drawer">

toolbar is your toolbar component which I mentioned earlier. toolbar是我之前提到的toolbar component

You will note that the navigation drawer is now listening to the drawer property.您会注意到导航抽屉现在正在侦听抽屉属性。

Please let me know if this answer is not suffice, I will create an example for you如果这个答案还不够,请告诉我,我将为您创建一个示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM