vue.js - Vuetify 抽屉导航工作一次然后停止

标签 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 .

应用程序.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>

头文件.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>

抽屉导航.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)
    }
  }
});

最佳答案

如果您不想使用 v-model ,您可以通过 :value 将其替换为值的绑定(bind)属性,如你所说。但是如果你这样做,你将不得不收听@input。事件,当值改变时引发。

在我看来,你可以通过替换 v-model 来实现你想要做的事情。对于 :value="drawerState"并通过绑定(bind)新方法updateDrawerState@input事件。

您的 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();
        }
      },
    },
},

关于vue.js - Vuetify 抽屉导航工作一次然后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48388834/

相关文章:

javascript - Vuetify 中是否有特定的数字输入组件?

unit-testing - 如何使用 vue/cli 进行 cypress 单元测试

javascript - Axios catch 错误返回 JavaScript 错误而不是服务器响应

javascript - 检查 JS/Vue 2 中是否按下 Shift + 字母

javascript - 如何在 nuxtjs 中映射子状态

html - 看不到最后的滚动 block

vue.js - 数据绑定(bind)整个页面的正确方法或 vuejs 方法是什么?

javascript - Vuex 提交 : JSON circular structure error

vue.js - 删除 Vuex 商店中的商品

javascript - 通过单击展开图像(vuetify)