Vuetify导航抽屉工作一次然后停止

时间:2018-01-22 19:22:23

标签: vue.js vuex vuetify.js

我正在使用Vuetify导航抽屉。打开它的元素位于标题中的单独组件(v-toobar-side-icon)中。我将抽屉的打开/关闭状态存储在商店中。我正在使用计算方法来获取抽屉的状态。我一直收到错误消息

Computed property "drawerState" was assigned to but it has no setter.

我知道这种情况正在发生,因为我必须使用v-model来控制抽屉的可见性。我不想使用计算的setter来改变抽屉的状态,我想使用click方法。

我尝试使用:value代替v-model,但我无法让它正常工作。我遇到的实际问题是导航抽屉控制(打开/关闭)在主页上的工作,但是一旦我导航到另一个页面,他们就会停止工作。如果我导航回主页,他们仍然无法正常工作。 getter和setter正在改变状态并更新它们应该的方式,但导航抽屉的isActive属性保持false

App.vue

<template lang="pug">
  v-app
    app-NavDrawer
    app-header
    router-view
    v-footer(app dark)
      span &copy; 2018 #[a(href="http://www.smellydogcoding.com") Smellydog Coding]
</template>

<script>
  import Header from './components/header/Header.vue'
  import NavDrawer from './components/header/NavDrawer.vue'
  export default {
    data () {
      return {

      }
    },
    components: {
      appHeader: Header,
      appNavDrawer: NavDrawer
    }
  }
</script>

<style>
html {
  overflow-y: auto;
}
a {
  text-decoration: none;
}
 footer {
   color: white;
 }
</style>

Header.vue

<template lang="pug">
  v-toolbar.mt-0(dark)
    v-toolbar-side-icon(@click.stop="openDrawer")
    router-link(to="/" tag="v-toolbar-title") Pool Math
</template>

<script>

export default {
  methods: {
    openDrawer() {
      this.$store.dispatch('navDrawer','open');
    }
  }
}
</script>

<style scoped>
  .toolbar__title {
    cursor: pointer;
  }
</style>

NavDrawer.vue

<template lang="pug">
  v-navigation-drawer(v-model="drawerState" dark fixed temporary)
    v-list.pa-1
      v-list-tile(avatar tag="div")
        v-list-tile-avatar
          img(src="../../../public/v.png")
        v-list-tile-content
          v-list-tile-title Guest
        v-list-tile-action
          v-btn(icon @click.stop="closeDrawer")
            v-icon close
    v-list.pt-0(dense)
      v-divider(light)
      router-link(to="/s1p0" tag="v-list-tile" active-class="active").expansion-panel__header.how-to
        v-list-tile-content
          v-list-tile-title.text-xs-center How to Use This Website
      v-expansion-panel
        v-expansion-panel-content
          div(slot="header") Section 1 - Conversions
          router-link(to="/s1p0" tag="v-list-tile" active-class="active")
            v-list-tile-content
              v-list-tile-title 1.0 - Useful Conversions
          router-link(to="/s1p1" tag="v-list-tile" active-class="active")
            v-list-tile-content
              v-list-tile-title 1.1 - Convert ounces to pounds
          router-link(to="/s1p2" tag="v-list-tile" active-class="active")
            v-list-tile-content
              v-list-tile-title 1.2 - Convert fluid ounces to gallons
          router-link(to="/s1p3" tag="v-list-tile" active-class="active")
            v-list-tile-content
              v-list-tile-title 1.3 - Convert fluid ounces to cups
          router-link(to="/s1p4" tag="v-list-tile" active-class="active")
            v-list-tile-content
              v-list-tile-title 1.4 - Convert inches to feet
        v-expansion-panel-content
          div(slot="header") Section 2 - Area and Volume
          router-link(to="/s2p0" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 2.0 - Introduction to This Section
          router-link(to="/s2p1" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 2.1 - Area of a Swimming Pool
          router-link(to="/s2p2" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 2.2 - Area of a Hot Tub
          router-link(to="/s2p3" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 2.3 - Volume of Water in a Swimming Pool
          router-link(to="/s2p4" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 2.4 - Volume of Water in a Multi-Depth Pool
          router-link(to="/s2p5" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 2.5 - Volume of Water in a Hot Tub
          router-link(to="/s2p6" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 2.6 - Volume of Water in a Hot Tub with Seats
          router-link(to="/s2p7" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 2.7 - Volume of Water Loss in a Swimming Pool
          router-link(to="/s2p8" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 2.8 - Volume of Water Loss in a Hot Tub
        v-expansion-panel-content
          div(slot="header") Section 3 - Water Balance
          router-link(to="/s3p0" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 3.0 - Introduction to This Section
          router-link(to="/s3p1" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 3.1 - Calculate Saturation Index
          router-link(to="/s3p2" tag="v-list-tile" active-class="active")
            v-list-tile-content
                v-list-tile-title 3.2 - Calculate Saturation Index - With CA
</template>

<script>
export default {
  computed: {
    drawerState() {
      return this.$store.getters.navDrawer; 
    }
  },
  methods: {
    closeDrawer() { this.$store.dispatch('navDrawer','close')}
  }
}
</script>

<style scoped>
  aside {
    overflow-y: auto;
  }
  .navigation-drawer {
    padding: 0 0 1.0rem;
  }
  .how-to {
    border-bottom: 1px solid rgba(255,255,255,0.12);
  }
  .how-to .list__tile__title {
    font-size: 1.15rem;
  }
</style>

store.js

import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';

Vue.use(Vuex);

export const store = new Vuex.Store({
  state: {
    drawer: false
  },
  getters: {  // send state to a component
    navDrawer: state => {
      return state.drawer
    }
  },
  mutations: { // modify state synchronously
    navDrawer: (state, command) => {
      command === 'open' ? state.drawer = true : state.drawer = false;
    }
  },
  actions: { // modify state aschronously
    navDrawer: ({commit}, command) => {
      commit('navDrawer', command)
    }
  }
});

3 个答案:

答案 0 :(得分:2)

如果您不想使用v-model,可以通过:value属性将值替换为值,如您所述。但是如果你这样做,你将不得不听取@input事件,当值发生变化时会引发事件。

在我看来,您可以通过将v-model替换为:value="drawerState"并将新方法updateDrawerState绑定到@input来实现您的目标。 {1}}事件。

您的NavDrawer.vue将以

开头
<template lang="pug">
  v-navigation-drawer(:value="drawerState" @input="updateDrawerState" dark fixed temporary)

您必须在同一个组件中添加此方法:

updateDrawerState(state) {
  if (state) {
    this.closeDrawer();
  }
}

仅供参考:你可以通过在计算机中添加一个setter来实现几乎相同的东西:

computed: {
    drawerState: {
      get() {
        return this.$store.getters.navDrawer; 
      },
      set(state) {
        if (state) {
          this.closeDrawer();
        }
      },
    },
},

答案 1 :(得分:1)

我能够通过将汉堡包图标v-toolbar-side-icon从标题组件移动到导航抽屉组件来解决这个问题(虽然从技术上说这是一种解决方法):

v-toolbar-side-icon(@click="drawer = !drawer") v-navigation-drawer(v-model="drawer" dark fixed temporary width="350")

因为打开和关闭按钮现在位于我可以使用的相同组件中

@click="drawer = !drawer"

切换导航抽屉的可见性。

Thomas Ferro:

我确实尝试过您的解决方案,并且只需进行一次小修改就可以正常运行:

methods: {
    updateDrawerState(state) {
      if (!state) { this.closeDrawer(); }
      else { this.openDrawer(); }
      console.log(state)
    },
    openDrawer() { this.$store.dispatch('navDrawer','open') },
    closeDrawer() { this.$store.dispatch('navDrawer','close') }
  }

当我尝试使用

updateDrawerState(state) {
  if (state) {
    this.closeDrawer();
  }
}

当我点击汉堡包时,导航抽屉将开始打开,然后立即关闭。我认为发生这种情况是因为只要你点击汉堡包打开它就会改变状态,每个状态变化都会关闭抽屉。通过检查状态然后根据状态触发openDrawercloseDrawer,一切都按预期工作。

感谢您提出:value以及更多建议,指出我需要收听@input(我发现的:value的其他帖子没有提到这一点。)非常感谢你!

答案 2 :(得分:1)

如果有人正在寻找一种让Vuetify的导航抽屉使用Vuex状态属性的方法,这就是我如何管理这个案例:避免崩溃应用程序的无限循环的秘诀是对setter方法有条件计算属性,确保仅当组件的$store.state值与v-model中的值不同时才会更改$store.state属性。

  computed: {
    navigation: {
      get () {
        return this.$store.state.navigation
      },
      set (state) {
        if (state !== this.$store.state.navigation) {
          this.$store.dispatch('toggleNavigation')
        }
      }
    }
  },