export const useMainStore = defineStore('main', {
state: () => ({
routers: []
}),
actions: {
pushRouter(router) {
this.routers.push(router)
},
logout(router) {
// 清空角色、token、
// 重制左侧菜单和tabview
localStorage.removeItem('token')
localStorage.removeItem('ui')
localStorage.removeItem('routers')
useRouter().push('/login')
}
}
})
最终我这样解决了,传入router实例
其它.vue页面
let router = useRouter()
useMainStore().logout(router)
pinia文件
export const useMainStore = defineStore('main', {
actions: {
logout(router) {
localStorage.removeItem('token')
localStorage.removeItem('ui')
localStorage.removeItem('routers')
router.push('/login')
}
}
useRouter()要在vue组件里写,其他地方需要可以用路由实例来跳转,比如