vuex中使用modules時(shí)遇到的坑及問(wèn)題
vuex使用modules時(shí)遇到的坑
其實(shí)也不算坑,只是自己沒(méi)注意看官網(wǎng)api,定義module另外命名時(shí),需要在module中加一個(gè)命名空間namespaced: true
屬性,否則命名無(wú)法暴露出來(lái),導(dǎo)致報(bào)[vuex] module namespace not found in mapState()等錯(cuò)誤。
vuex中modules基本用法
1. store文件結(jié)構(gòu)
- src - components - store ? ? -index.js ? ? -modules ? ? ? ? -app.js ? ? ? ? -bus.js
2.1 index.js中-手動(dòng)引入modules
import Vue from 'vue' import Vuex from 'vuex' import bus from './module/bus' import app from './module/app' Vue.use(Vuex) export default new Vuex.Store({ ? ? state: { ? ? ? ? // 這里是根vuex狀態(tài) ? ? }, ? ? mutations: { ? ? ? ? // 這里是根vuex狀態(tài) ? ? }, ? ? actions: { ? ? ? ? // 這里是根vuex狀態(tài) ? ? }, ? ? modules: { // 子vuex狀態(tài)模塊注冊(cè) ? ? ? ? namespaced: true, // 為了解決不同模塊命名沖突的問(wèn)題 ? ? ? ? app, ? ? ? ? bus ? ? } })
2.2 index.js中-動(dòng)態(tài)引入modules
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const dynamicModules = {} const files = require.context('.', true, /\.js$/) const dynamicImportModules = (modules, file, splits, index = 0) => { ? ? if (index == 0 && splits[0] == 'modules') { ? ? ? ? ++index ? ? } ? ? if (splits.length == index + 1) { ? ? ? ? if ('index' == splits[index]) { ? ? ? ? ? ? modules[splits[index - 1]] = files(file).default ? ? ? ? } else { ? ? ? ? ? ? modules.modules[splits[index]] = files(file).default ? ? ? ? } ? ? } else { ? ? ? ? let tmpModules = {} ? ? ? ? if ('index' == splits[index + 1]) { ? ? ? ? ? ? tmpModules = modules ? ? ? ? } else { ? ? ? ? ? ? modules[splits[index]] = modules[splits[index]] ? modules[splits[index]] : {namespaced: true, modules: {}} ? ? ? ? ? ? tmpModules = modules[splits[index]] ? ? ? ? } ? ? ? ? dynamicImportModules(tmpModules, file, splits, ++index) ? ? } } files.keys().filter(file => file != './index.js').forEach(file => { ? ? let splits = file.replace(/(\.\/|\.js)/g, '').split('\/'); ? ? dynamicImportModules(dynamicModules, file, splits) }) export default new Vuex.Store({ ? ? modules: dynamicModules, ? ? strict: process.env.NODE_ENV !== 'production' })
3. app.js文件內(nèi)容
const state = { ? ? user: {}, // 需要管理的狀態(tài)數(shù)據(jù) } const mutations = { ? ? setUser (state, val) { ? ? ? ? ? ? state.user = val ? ? ? ? } } const getters = {} const actions = {} export default { ? ? namespaced: true, ? ? state, ? ? mutations, ? ? getters, ? ? actions }
4.1 使用 a.vue頁(yè)面
// 使用模塊中的mutations、getters、actions時(shí)候,要加上模塊名,例如使用commint執(zhí)行mutations時(shí) // 格式: 模塊名/模塊中的mutations this.$store.commit("app/setUser", user) // 獲取屬性時(shí)同樣加上模塊名 this.$store.state.app.user?
4.2 utils.js中使用
// 引入 這里的store 就相當(dāng)于頁(yè)面中的 this.$store import store from '@/store' export const setCurUser = (user) => { ? ? let curUser = store.app.user ? ? if(!curUser) { ? ? ? ? store.commit("app/setUser", user) ? ? ? ? return user ? ? } ? ?? ? ? return curUser }
5. 配置main.js
import Vue from 'vue' import App from './App' import router from './router' import store from './store' new Vue({ ? el: '#app', ? router, ? store, ? render: h => h(App) })
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue-cli + webpack 新建項(xiàng)目出錯(cuò)的問(wèn)題
下面小編就為大家分享一篇解決vue-cli + webpack 新建項(xiàng)目出錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03vue查詢數(shù)據(jù)el-table不更新數(shù)據(jù)的解決方案
這篇文章主要介紹了vue查詢數(shù)據(jù)el-table不更新數(shù)據(jù)的問(wèn)題及解決方案,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12vue中使用loadsh的debounce防抖函數(shù)問(wèn)題
這篇文章主要介紹了vue中使用loadsh的debounce防抖函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11vue2.0設(shè)置proxyTable使用axios進(jìn)行跨域請(qǐng)求的方法
這篇文章主要介紹了vue2.0設(shè)置proxyTable使用axios進(jìn)行跨域請(qǐng)求,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10