[英]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.