vue项目中,经常会使用到vuex,vuex是vue的一个状态管理。
本文简单总结一下:vuex中mapMutations的使用。
通过多个方法操作数据,那么,就可以使用mapMutations集中管理。
vuex中声明变量个方法
1.从"vuex中引入
import { mapMutations, mapActions, mapGetters } from "vuex";
2.在methods中引入
methods: {
...mapMutations([
"setBreadCrumb",
"setTagNavList",
"addTag",
"setLocal",
"setHomeRoute"
]),
3,调用方法
mounted() {
this.setBreadCrumb(this.$route);
this.setTagNavList(getNewTagList(this.tagNavList, newRoute));
// 设置初始语言
this.setLocal(this.$i18n.locale);
this.addTag({
route: this.$store.state.app.homeRoute
});
this.setHomeRoute(routers);
}
4,store/module/app.js中:
mutation里的方法
import {
getBreadCrumbList,
setTagNavListInLocalstorage,
getMenuByRouter,
getTagNavListFromLocalstorage,
getHomeRoute,
getNextRoute,
routeHasExist,
routeEqual,
getRouteTitleHandled,
localSave,
localRead
} from '@/libs/util'
import beforeClose from '@/router/before-close'
import { saveErrorLogger } from '@/api/data'
import router from '@/router'
import routers from '@/router/routers'
import config from '@/config'
const { homeName } = config
const closePage = (state, route) => {
const nextRoute = getNextRoute(state.tagNavList, route)
state.tagNavList = state.tagNavList.filter(item => {
return !routeEqual(item, route)
})
router.push(nextRoute)
}
export default {
state: {
breadCrumbList: [],
tagNavList: [],
homeRoute: {},
local: localRead('local'),
errorList: [],
hasReadErrorPage: false
},
getters: {
menuList: (state, getters, rootState) => getMenuByRouter(routers, rootState.user.access),
errorCount: state => state.errorList.length
},
mutations: {
setBreadCrumb (state, route) {
state.breadCrumbList = getBreadCrumbList(route, state.homeRoute)
},
setHomeRoute (state, routes) {
state.homeRoute = getHomeRoute(routes, homeName)
},
setTagNavList (state, list) {
let tagList = []
if (list) {
tagList = [...list]
} else tagList = getTagNavListFromLocalstorage() || []
if (tagList[0] && tagList[0].name !== homeName) tagList.shift()
let homeTagIndex = tagList.findIndex(item => item.name === homeName)
if (homeTagIndex > 0) {
let homeTag = tagList.splice(homeTagIndex, 1)[0]
tagList.unshift(homeTag)
}
state.tagNavList = tagList
setTagNavListInLocalstorage([...tagList])
},
closeTag (state, route) {
let tag = state.tagNavList.filter(item => routeEqual(item, route))
route = tag[0] ? tag[0] : null
if (!route) return
if (route.meta && route.meta.beforeCloseName && route.meta.beforeCloseName in beforeClose) {
new Promise(beforeClose[route.meta.beforeCloseName]).then(close => {
if (close) {
closePage(state, route)
}
})
} else {
closePage(state, route)
}
},
addTag (state, { route, type = 'unshift' }) {
let router = getRouteTitleHandled(route)
if (!routeHasExist(state.tagNavList, router)) {
if (type === 'push') state.tagNavList.push(router)
else {
if (router.name === homeName) state.tagNavList.unshift(router)
else state.tagNavList.splice(1, 0, router)
}
setTagNavListInLocalstorage([...state.tagNavList])
}
},
setLocal (state, lang) {
localSave('local', lang)
state.local = lang
},
addError (state, error) {
state.errorList.push(error)
},
setHasReadErrorLoggerStatus (state, status = true) {
state.hasReadErrorPage = status
}
},
actions: {
addErrorLog ({ commit, rootState }, info) {
if (!window.location.href.includes('error_logger_page')) commit('setHasReadErrorLoggerStatus', false)
const { user: { token, userId, userName } } = rootState
let data = {
...info,
time: Date.parse(new Date()),
token,
userId,
userName
}
// saveErrorLogger(info)
saveErrorLogger(info).then(() => {
commit('addError', data)
})
}
}
}
mapMutations可以看作vuex的mutations方法的映射,其实也是调用 this.$store.commit()方法
改变数据状态,只不过比起mutations可以更方便操作多个,处理比较优雅简练,当然若有其他优点,
请在评论区多多讨论。