简体   繁体   English

我正在切换侧边栏的“未知突变类型:mobilenav/showmobilenav”

[英]I am getting `unknown mutation type: mobilenav/showmobilenav` toggling a sidebar

I am trying to toggle a sidebar, trying to commit a mutation when clicking a button but I keep getting [vuex] unknown mutation type: mobilenav/showmobilenav error every time.我正在尝试切换侧边栏,尝试在单击按钮时提交突变,但我每次都会收到[vuex] unknown mutation type: mobilenav/showmobilenav错误。

I have tried everything I have seen online, but nothing works.我已经尝试了我在网上看到的所有内容,但没有任何效果。

I am also trying to use the module mode instead of classic mode so, my store is structured that way below.我也在尝试使用模块模式而不是经典模式,所以我的商店的结构如下。

so below are my stores所以下面是我的商店

store/index.js存储/index.js

export const state = () => ({
})
 
export const mutations = ({
})
 
export const actions = ({
})
 
export const getters = ({
})

store/mobilenav.js商店/mobilenav.js

export const state = () => ({
    mobilenav: false
})

export const mutations = () => ({
    showmobilenav(state) {
        state.mobilenav = true;
    },
    hidemobilenav(state) {
        state.mobilenav = false;
    }
})

export const getters = () => ({
    ismobilenavvisible(state) {
        return state.dropdown;
    }
})

the VUE file that calls the mutation on clicking a button单击按钮时调用突变的 VUE 文件

<template>
    <div class="bb" @click="showsidenav">
        <img src="~/assets/svg/burgerbar.svg" alt="" />
    </div>
</template>

<script>
export default {
    methods: {
        showsidenav() {
            this.$store.commit("mobilenav/showmobilenav");
            console.log("sidenav shown");
        },
    },
}
</script>

<style scoped>

</style>

You need to do this in your store/index.js :您需要在store/index.js中执行此操作:

import Vue from 'vue'
import Vuex from 'vuex'
import mobilenav from './mobilenav'

Vue.use(Vuex)

const createStore = () => new Vuex.Store({
  modules: {
    mobilenav,
  },
})

export default createStore

Then inject the store when you load your vue app:然后在加载 vue 应用程序时注入 store:

import Vue from 'vue'
import App from '~/pages/App.vue'
import createStore from '~/store'

document.addEventListener('DOMContentLoaded', () => {
  if (document.getElementById('app-wrapper')) {
    new Vue({
      store: createStore(),
      render: h => h(App),
    }).$mount('#app-wrapper')
  }
})

The problem is the exported mutations (and getters ) is a function, but it needs to be an object:问题是导出的mutations (和getters )是 function,但它必须是 object:

store/mobilenav.js:商店/mobilenav.js:

// export const mutations = () => ({/*...*/}) ❌
export const mutations = {/*...*/} ✅

// export const getters = () => ({/*...*/}) ❌
export const getters = {/*...*/} ✅

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

相关问题 我在 redux 的调度之间收到 state 错误的突变 - I am getting mutation of state error between dispatches from redux 为什么我在 Apollo Client 中出现 400 错误 - Why am I getting a 400 error in mutation in Apollo Client 我正在获取音频/视频:未知的MIME类型。 在IE 11中的视频标签mp4中 - I am getting AUDIO/VIDEO: Unknown MIME type. in video tag mp4 in IE 11 类型为“ Mutation \\”的字段“ forgotPassword \\”上的“未知参数\\” email \\”。” - “Unknown argument \”email\“ on field \”forgotPassword\“ of type \”Mutation\“.” 注册模块中的vuex未知突变类型 - vuex unknown mutation type in registered module 如何修复 Vuex 中未知突变类型的错误? - How to fix the error of unknown mutation type in Vuex? 我在使用 jQuery 切换侧边栏时遇到问题 - I've got a problem with a toggling sidebar using jQuery 在 app.js 中导入 js 文件时出现未知文件扩展名错误,使用类型:package.json 中的模块 - I am getting unknown file extension error while importing a js file in app.js, using type: module in package.json 为什么在执行更改时会收到“无法为不可为空的字段返回 null”错误? - Why am I getting a “Cannot return null for non-nullable field” error when doing a mutation? 在Bootstrap中切换覆盖的侧边栏 - Toggling overlayed sidebar in Bootstrap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM