在vue-router4的全局守卫中使用vuex里的方法

时间:2025-05-15 22:25:26
//store/ import { createStore } from 'vuex' import global from './global'; import student from './student' // Create a new store instance. const store = createStore({ modules: { glo: global, stu: student } }) //将store store 导出去 export function myStore () { return store; } export default store; //router/ import { createRouter, createWebHashHistory } from 'vue-router' import {myStore} from '@/store/index' const store = myStore(); const router = createRouter({ history: createWebHashHistory(), routes: [ ...路由 ], }) //路由全局前置守卫 router.beforeEach((to,from,next)=>{ //当前往登陆页的时候开启全屏 前往其他页面关闭全屏 if(to.name==='login'){ store.commit('glo/cutShowFull',true) //在这里能用到store 的commit 那么其他方法也是能使用的 就不一一举例了 }else{ store.commit('glo/cutShowFull',false); }; next() }) export default router

相关文章