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 理解之白話(huà) getter/setter詳解
這篇文章主要介紹了Vue getter setter,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue不用window的方式如何刷新當(dāng)前頁(yè)面
這篇文章主要介紹了vue不用window的方式如何刷新當(dāng)前頁(yè)面,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Vue是怎么渲染template內(nèi)的標(biāo)簽內(nèi)容的
這篇文章主要介紹了Vue是怎么渲染template內(nèi)的標(biāo)簽內(nèi)容的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
vue如何通過(guò)params和query傳值(刷新不丟失)
這篇文章主要介紹了vue如何通過(guò)params和query傳值(刷新不丟失),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue3+ts+EsLint+Prettier規(guī)范代碼的方法實(shí)現(xiàn)
本文主要介紹在Vue3中使用TypeScript做開(kāi)發(fā)時(shí),如何安裝與配置EsLint和Prettier,以提高編碼規(guī)范。感興趣的可以了解一下2021-10-10
vite+vue3+element-plus項(xiàng)目搭建的方法步驟
因?yàn)関ue3出了一段時(shí)間了,element也出了基于vue3.x版本的element-plus,vite打包聽(tīng)說(shuō)很快,嘗試一下,感興趣的可以了解一下2021-06-06
詳解vue使用vue-layer-mobile組件實(shí)現(xiàn)toast,loading效果
這篇文章主要介紹了詳解vue使用vue-layer-mobile組件實(shí)現(xiàn)toast,loading效果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

