vue3 vite pinia配置動態(tài)路由、解決刷新頁面路由消失的問題
實現(xiàn)思路
1,需要在靜態(tài)路由的基礎(chǔ)上用vue路由上面的addRoute()方法來動態(tài)添加路由,最后生成我們需要的路由
2,封裝添加路由的方法,在刷新頁面時重新生成路由
首先創(chuàng)建靜態(tài)路由
// 靜態(tài)路由 export const basicRoutes = [ { path: '/', redirect: '/login', }, { path: '/login', name: 'login', component: () => import('@/views/login/index.vue'), }, // 免登錄跳轉(zhuǎn)到項目中 { path: '/hideLogin', name: 'hideLogin', component: () => import('@/views/hideLogin/index.vue'), }, { // 異常頁面 path: '/errorPage', component: () => import('@/components/error-page/404'), name: '404', }, ]
創(chuàng)建路由的時候引入
import { createRouter, createWebHistory } from 'vue-router' import { basicRoutes as routes } from './routes' // 引入靜態(tài)路由 export const router = createRouter({ history: createWebHistory('/'), routes, scrollBehavior: () => ({ left: 0, top: 0 }), }) export default router
封裝處理路由方法
import { deepClone } from '@/utils/utils' // 深拷貝 import router from './index' // 處理路由展示列表樹形格式 (如果所有路由數(shù)據(jù)是在同一級,需要調(diào)用這個方法) export const formatRouterTree = (data) => { let parents = data.filter((i) => i.pid === 0), children = data.filter((item) => item.pid !== 0) dataToTree(parents, children) function dataToTree(parents, children) { parents.map((p) => { children.map((c, i) => { let _c = deepClone(children) _c.splice(i, 1) dataToTree([c], _c) if (p.children) { p.children.push(c) } else { p.children = [c] } }) }) } } // vite中獲取文件 const modules = import.meta.glob([ '../views/*.vue', '../views/*/*.vue', '../views/*/*/*.vue', '../components/layout/index.vue', ]) // 處理路由所需格式 export const generateRouter = (userRouters) => { let newRouter = null if (userRouters) newRouter = userRouters.map((i) => { let routes = { path: i.pathUrl, name: i.name, // meta: i.meta, component: i.pathUrl === '/layout' ? modules[`../components/layout/index.vue`] : modules[`../views${i.pathUrl}/index.vue`], } if (i.children) { routes.children = generateRouter(i.children) } return routes }) return newRouter } /** * 添加動態(tài)路由 */ export function setAddRoute(routes) { if (routes && routes.length > 0) routes.forEach((route) => { const routeName = route.name if (!router.hasRoute(routeName)) router.addRoute(route) }) }
在store中寫一個調(diào)用方法
import { defineStore } from 'pinia' import { generateRouter, setAddRoute } from '@/router/vue-router.js' export const routeStore = defineStore('route', { state: () => { return { } }, actions: { addRouter() { // 生成路由樹 // routerList在登陸成功時獲取到,在跳轉(zhuǎn)頁面之前存起來 let find = JSON.parse(window.localStorage.getItem('routerList')) let routerList = generateRouter(find) // 添加路由 setAddRoute(routerList) }, }, getters: {}, })
登陸時存儲路由信息
import { generateRouter, setAddRoute } from '@/router/vue-router.js' Login.Login(params).then(async (res) => { if (res.code === 200) { // 存用戶信息、token等,這里不寫了 // 調(diào)接口獲取路由信息 params傳當前用戶角色來獲取對應(yīng)的路由信息 let routerList = await getRouterList(params) // 存儲路由信息 window.localStorage.setItem('routerList', routerList) // 生成路由樹 let list = generateRouter(routerList) // 添加路由 setAddRoute(list) // 最后跳轉(zhuǎn)到首頁并提示 router.push('首頁') ElMessage.success('登陸成功') } })
解決刷新消失
上面只是在登陸時候添加了路由,點擊刷新頁面后會消失,需要在main.js中配置一下
import { createPinia } from 'pinia' import { routeStore } from '@/store/modules/routeMenu.js' // 這個是我的store里面的方法路徑 const store = createPinia() const app = createApp(App) app.use(store) let routeStores = routeStore() const addRouter = () => { routeStores.addRouter() } addRouter() // router要在添加完路由之后引入,不然還沒添加,路由已經(jīng)生成了 app.use(router)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中關(guān)于v-for循環(huán)key值問題的研究
這篇文章主要介紹了vue中關(guān)于v-for循環(huán)key值問題的研究,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06VUE前端實現(xiàn)token的無感刷新3種方案(refresh_token)
這篇文章主要給大家介紹了關(guān)于VUE前端實現(xiàn)token的無感刷新3種方案(refresh_token)的相關(guān)資料,為了提供更好的用戶體驗,我們可以通過實現(xiàn)Token的無感刷新機制來避免用戶在使用過程中的中斷,需要的朋友可以參考下2023-11-11一步步從Vue3.x源碼上理解ref和reactive的區(qū)別
vue3的數(shù)據(jù)雙向綁定,大家都明白是proxy數(shù)據(jù)代理,但是在定義響應(yīng)式數(shù)據(jù)的時候,有ref和reactive兩種方式,如果判斷該使用什么方式,是大家一直不很清楚地問題,下面這篇文章主要給大家介紹了關(guān)于從Vue3.x源碼上理解ref和reactive的區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-02-02Vue使用路由鉤子攔截器beforeEach和afterEach監(jiān)聽路由
這篇文章主要介紹了Vue使用路由鉤子攔截器beforeEach和afterEach監(jiān)聽路由,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11把vue-router和express項目部署到服務(wù)器的方法
下面小編就為大家分享一篇把vue-router和express項目部署到服務(wù)器的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02vue+elementui+vuex+sessionStorage實現(xiàn)歷史標簽菜單的示例代碼
本文主要介紹了vue+elementui+vuex+sessionStorage實現(xiàn)歷史標簽菜單的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12