欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3 vite pinia配置動(dòng)態(tài)路由、解決刷新頁(yè)面路由消失的問題

 更新時(shí)間:2023年10月23日 09:13:36   作者:解二  
這篇文章主要介紹了vue3 vite pinia配置動(dòng)態(tài)路由、解決刷新頁(yè)面路由消失的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

實(shí)現(xiàn)思路

1,需要在靜態(tài)路由的基礎(chǔ)上用vue路由上面的addRoute()方法來動(dòng)態(tài)添加路由,最后生成我們需要的路由

2,封裝添加路由的方法,在刷新頁(yè)面時(shí)重新生成路由

首先創(chuàng)建靜態(tài)路由

// 靜態(tài)路由 
export const basicRoutes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
  },
  // 免登錄跳轉(zhuǎn)到項(xiàng)目中
  {
    path: '/hideLogin',
    name: 'hideLogin',
    component: () => import('@/views/hideLogin/index.vue'),
  },
  {
    // 異常頁(yè)面
    path: '/errorPage',
    component: () => import('@/components/error-page/404'),
    name: '404',
  },
]

創(chuàng)建路由的時(shí)候引入

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ù)是在同一級(jí),需要調(diào)用這個(gè)方法)
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
}
 
/**
 * 添加動(dòng)態(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中寫一個(gè)調(diào)用方法

import { defineStore } from 'pinia'
import { generateRouter, setAddRoute } from '@/router/vue-router.js'
export const routeStore = defineStore('route', {
  state: () => {
    return {
    }
  },
  actions: {
    addRouter() {
      // 生成路由樹
      // routerList在登陸成功時(shí)獲取到,在跳轉(zhuǎn)頁(yè)面之前存起來
      let find = JSON.parse(window.localStorage.getItem('routerList'))
      let routerList = generateRouter(find)
      // 添加路由
      setAddRoute(routerList)
    },
  },
  getters: {},
})

登陸時(shí)存儲(chǔ)路由信息

import { generateRouter, setAddRoute } from '@/router/vue-router.js'
 
Login.Login(params).then(async (res) => {
     if (res.code === 200) {
        // 存用戶信息、token等,這里不寫了
 
        // 調(diào)接口獲取路由信息 params傳當(dāng)前用戶角色來獲取對(duì)應(yīng)的路由信息
        let routerList = await getRouterList(params)
        // 存儲(chǔ)路由信息
        window.localStorage.setItem('routerList', routerList)
        
        // 生成路由樹
        let list = generateRouter(routerList)
        // 添加路由
        setAddRoute(list)
        
        // 最后跳轉(zhuǎn)到首頁(yè)并提示
        router.push('首頁(yè)')
        ElMessage.success('登陸成功')
     }  
})

解決刷新消失

上面只是在登陸時(shí)候添加了路由,點(diǎn)擊刷新頁(yè)面后會(huì)消失,需要在main.js中配置一下

import { createPinia } from 'pinia'
import { routeStore } from '@/store/modules/routeMenu.js' // 這個(gè)是我的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é)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue表單數(shù)據(jù)修改與刪除功能實(shí)現(xiàn)

    Vue表單數(shù)據(jù)修改與刪除功能實(shí)現(xiàn)

    本文通過實(shí)例代碼介紹了Vue表單數(shù)據(jù)修改與刪除功能實(shí)現(xiàn),結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友跟隨小編一起看看吧
    2023-10-10
  • Vue3中使用Pinia的方法詳細(xì)介紹

    Vue3中使用Pinia的方法詳細(xì)介紹

    這篇文章主要給大家介紹了關(guān)于Vue3中使用Pinia的相關(guān)資料,pinia是一個(gè)用于vue的狀態(tài)管理庫(kù),類似于vuex,是vue的另一種狀態(tài)管理工具,文中介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • vue中vite.config.js配置跨域以及環(huán)境配置方式

    vue中vite.config.js配置跨域以及環(huán)境配置方式

    這篇文章主要介紹了vue中vite.config.js配置跨域以及環(huán)境配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue-router beforeEach跳轉(zhuǎn)路由驗(yàn)證用戶登錄狀態(tài)

    vue-router beforeEach跳轉(zhuǎn)路由驗(yàn)證用戶登錄狀態(tài)

    這篇文章主要介紹了vue-router beforeEach跳轉(zhuǎn)路由驗(yàn)證用戶登錄狀態(tài),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • Vue使用Axios庫(kù)請(qǐng)求數(shù)據(jù)時(shí)跨域問題的解決方法詳解

    Vue使用Axios庫(kù)請(qǐng)求數(shù)據(jù)時(shí)跨域問題的解決方法詳解

    在 VUE 項(xiàng)目開發(fā)時(shí),遇到個(gè)問題,正常設(shè)置使用 Axios 庫(kù)請(qǐng)求數(shù)據(jù)時(shí),報(bào)錯(cuò)提示跨域問題,那在生產(chǎn)壞境下,該去怎么解決呢?下面小編就來和大家詳細(xì)講講
    2024-01-01
  • Vue3處理錯(cuò)誤邊界(error boundaries)的示例代碼

    Vue3處理錯(cuò)誤邊界(error boundaries)的示例代碼

    在開發(fā) Vue 3 應(yīng)用時(shí),處理錯(cuò)誤邊界(Error Boundaries)是一個(gè)重要的考量,在 Vue 3 中實(shí)現(xiàn)錯(cuò)誤邊界的方式與 React 等其他框架有所不同,下面,我們將深入探討 Vue 3 中如何實(shí)現(xiàn)錯(cuò)誤邊界,并提供一些示例代碼幫助理解什么是錯(cuò)誤邊界,需要的朋友可以參考下
    2024-10-10
  • vue+element開發(fā)一個(gè)谷歌插件的全過程

    vue+element開發(fā)一個(gè)谷歌插件的全過程

    這篇文章主要給大家介紹了關(guān)于vue+element開發(fā)一個(gè)谷歌插件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • vue中el-table兩個(gè)表尾合計(jì)行聯(lián)動(dòng)同步滾動(dòng)條實(shí)例代碼

    vue中el-table兩個(gè)表尾合計(jì)行聯(lián)動(dòng)同步滾動(dòng)條實(shí)例代碼

    項(xiàng)目開發(fā)中遇到一個(gè)比較兩個(gè)form差異的需求,但當(dāng)item過多就需要滾動(dòng)條,下面這篇文章主要給大家介紹了關(guān)于vue中el-table兩個(gè)表尾合計(jì)行聯(lián)動(dòng)同步滾動(dòng)條的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • vue內(nèi)置組件keep-alive事件動(dòng)態(tài)緩存實(shí)例

    vue內(nèi)置組件keep-alive事件動(dòng)態(tài)緩存實(shí)例

    這篇文章主要介紹了vue內(nèi)置組件keep-alive事件動(dòng)態(tài)緩存實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Vue組件化開發(fā)思考

    Vue組件化開發(fā)思考

    這篇文章主要介紹了Vue組件化開發(fā)的思考以及相關(guān)的原理介紹,如果你對(duì)此有興趣,可以學(xué)習(xí)參考下。
    2018-02-02

最新評(píng)論