最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3+vite動態(tài)加載路由,本地路由和線上路由匹配方式

 更新時間:2023年06月28日 15:14:00   作者:arno-cai  
這篇文章主要介紹了vue3+vite動態(tài)加載路由,本地路由和線上路由匹配方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

使用場景

本地全路由動態(tài)匹配線上路由

目的

本地路由方便開發(fā)配置,匹配線上路由,進(jìn)行頁面路由控制

內(nèi)容

// router/constRoutes.js
// ---------------------------------------
// constRoutes 固定需要加載的路由,
// notFound 動態(tài)路由加載完成后再添加,否則刷新頁面,此時還沒有路由,會直接定向到notFound頁面
// ---------------------------------------
export const constRoutes = [
? {
? ? path: '/',
? ? redirect: '/home'
? },
? {
? ? path: '/home',
? ? name: 'home',
? ? component: () => import('@/views/home/index.vue')
? },
? {
? ? path: '/login',
? ? name: 'login',
? ? component: () => import('@/views/login/index.vue')
? }
]
export const notFound = {
? path: '/:pathMatch(.*)*',
? name: 'notFound',
? component: () => import('@/views/not-found/not-found.vue')
}
// router/asyncRoutes.js
// ---------------------------------------
// 注意:children下的path不需要加'/'
// meta下可以加各種本地你想要的參數(shù)
// ---------------------------------------
const Layout = () => import('@/layout/index.vue')
export const asyncRoutes = [
? {
? ? path: '/monitoringCenter',
? ? name: 'MonitoringCenter',
? ? component: Layout,
? ? meta: { title: '監(jiān)控中心' },
? ? children: [
? ? ? {
? ? ? ? path: 'carMonitoring',
? ? ? ? name: 'CarMonitoring',
? ? ? ? meta: { title: '車輛監(jiān)控' },
? ? ? ? children: [
? ? ? ? ? {
? ? ? ? ? ? path: 'positioning',
? ? ? ? ? ? name: 'Positioning',
? ? ? ? ? ? component: () => import('@/views/monitoringCenter/positioning/index.vue'),
? ? ? ? ? ? meta: { title: '實時定位' }
? ? ? ? ? }
? ? ? ? ]
? ? ? },
? ? ? {
? ? ? ? path: 'monitorSetting',
? ? ? ? name: 'MonitorSetting',
? ? ? ? meta: { title: '監(jiān)控設(shè)置' },
? ? ? ? children: [
? ? ? ? ? {
? ? ? ? ? ? path: 'ruleManagement',
? ? ? ? ? ? name: 'RuleManagement',
? ? ? ? ? ? component: () => import('@/views/monitoringCenter/ruleManagement/index.vue'),
? ? ? ? ? ? meta: { title: '報警規(guī)則' }
? ? ? ? ? }
? ? ? ? ]
? ? ? }
? ? ]
? },
// router/index.js
// ---------------------------------------
// 先加載constRoutes 固定路由
// ---------------------------------------
import { createRouter, createWebHistory } from 'vue-router'
import { constRoutes } from '@/router/constRoutes'
const router = createRouter({
? history: createWebHistory(import.meta.env.BASE_URL),
? routes: constRoutes
})
export default router
// utils/map-menus.js
// ---------------------------------------
//導(dǎo)出方法,menuList是線上路由,Routes第一次默認(rèn)是本地路由
// ---------------------------------------
import { asyncRoutes } from '@/router/asyncRoutes'
// 添加redirect重定向
let redirect = ''
// 匹配路由,添加對應(yīng)線上傳回來的參數(shù)
export function mapMenusToRoutes(menuList, Routes = asyncRoutes) {
? const routes = []
? for (let item of Routes) {
? ? const m = menuList.find((menu) => item.name == menu.name)
? ? if (m) {
? ? // 線上按鈕權(quán)限放在attr中
? ? ? m.attr && m.attr.length && (item.attr = m.attr)
? ? ? item.id = m.id
? ? ? // ‘?.'判斷是否有children且大于零
? ? ? if (item.children?.length) {
? ? ? ?? ?// 添加重定向
? ? ? ? if (item.path.includes('/')) {
? ? ? ? ? redirect = item.path
? ? ? ? ? item.redirect = `${redirect}/${item.children[0].path}`
? ? ? ? } else {
? ? ? ? ? item.redirect = `${redirect}/${item.path}/${item.children[0].path}`
? ? ? ? }
? ? ? ? // 如果有children 則回調(diào)并賦值
? ? ? ? item.children = mapMenusToRoutes(m.children, item.children)
? ? ? }
? ? ? routes.push(item)
? ? }
? }
? return routes
}

&&&&后面是具體調(diào)用方式,因人而異,可自行選擇

// stores/login.js
import { defineStore } from 'pinia'
import router from '@/router'
import { notFound } from '@/router/constRoutes'
import localCache from '@/utils/cache'
import { mapMenusToRoutes } from '@/utils/map-menus'
import { login, getInfo } from '@/api/user/login'
export const useLoginStore = defineStore({
? id: 'useLoginStore',
? state: () => ({
? ? token: '',
? ? menuList: [],
? ? routeMenus: []
? }),
? getters: {
? ? routeMenusGet() {
? ? ? return this.routeMenus
? ? }
? },
? actions: {
? ? async LoginAction(loginForm) {
? ? ? // 1.實現(xiàn)登錄邏輯
? ? ? const { data: loginData } = await login(loginForm)
? ? ? this.token = loginData.token
? ? ? // 保存至localStorage,方法隨意
? ? ? localCache.setCache('token', this.token)
? ? ? // 2.跳轉(zhuǎn)至首頁
? ? ? router.push('/home')
? ? },
? ? // router.beforeEach獲取菜單樹
? ? async getInfoAction() {
? ? ? const infoData = await getInfo({ token: this.token })
? ? ? this.menuList = infoData.data.menuList
? ? ? this.addRouteMenus()
? ? },
? ? // 動態(tài)加載路由
? ? addRouteMenus() {
? ? ? this.routeMenus = mapMenusToRoutes(this.menuList)
? ? ? // vue3中只有addRoute
? ? ? for (let item of this.routeMenus) {
? ? ? ? router.addRoute(item)
? ? ? }
? ? ? // 最后添加,否則刷新請求線上路由時,還沒有加載完,會直接定向到notFound
? ? ? router.addRoute(notFound)
? ? }
? }
})
// src/permission.js
// main.js中引入次文件 import '@/permission'
import router from './router'
import localCache from '@/utils/cache'
import { useLoginStore } from '@/stores/user/login'
router.beforeEach(async (to, from, next) => {
? document.title = `${to.meta.title}`
? if (to.path === '/login') {
? ?? ?// 清空localStorage
? ? localCache.clearCache()
? ? next()
? } else {
? ? const token = localCache.getCache('token')
? ? if (!token) {
? ? ? next(`/login`)
? ? } else {
? ? ? const loginStore = useLoginStore()
? ? ? if (loginStore.routeMenus.length) {
? ? ? ? next()
? ? ? } else {
? ? ? // 如果沒有路由,調(diào)用動態(tài)加載路由
? ? ? ? try {
? ? ? ? ? await loginStore.getInfoAction()
? ? ? ? ? next({ ...to, replace: true })
? ? ? ? } catch (error) {
? ? ? ? ? next(`/login`)
? ? ? ? }
? ? ? }
? ? }
? }
})

總結(jié)

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

相關(guān)文章

  • vue2和vue3組件v-model區(qū)別詳析

    vue2和vue3組件v-model區(qū)別詳析

    v-model通常用于input的雙向數(shù)據(jù)綁定,它并不會向子組件傳遞數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于vue2和vue3組件v-model區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • Vue2.0 slot分發(fā)內(nèi)容與props驗證的方法

    Vue2.0 slot分發(fā)內(nèi)容與props驗證的方法

    本篇文章主要介紹了Vue2.0 slot分發(fā)內(nèi)容與props驗證的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • Vue之封裝公用變量以及實現(xiàn)方式

    Vue之封裝公用變量以及實現(xiàn)方式

    這篇文章主要介紹了Vue之封裝公用變量以及實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Vite實現(xiàn)圖片壓縮的四種有效方法

    Vite實現(xiàn)圖片壓縮的四種有效方法

    原主流插件 vite-plugin-imagemin 已不兼容 Vite5,會導(dǎo)致構(gòu)建失敗,本文整理了 4 種適用于 Vite5 及以下版本的圖片壓縮方案,涵蓋插件化與自定義腳本,兼顧易用性與靈活性,需要的朋友可以參考下
    2025-08-08
  • vue3如何使用ref獲取元素

    vue3如何使用ref獲取元素

    這篇文章主要介紹了vue3如何使用ref獲取元素,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • vue3子組件如何修改父組件傳過來的props數(shù)據(jù)

    vue3子組件如何修改父組件傳過來的props數(shù)據(jù)

    周所周知vue的props是單向數(shù)據(jù)流,可以從父組件中改變傳往子組件的props,反之則不行,下面這篇文章主要給大家介紹了關(guān)于vue3子組件如何修改父組件傳過來的props數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • vue實現(xiàn)自定義樹形組件的示例代碼

    vue實現(xiàn)自定義樹形組件的示例代碼

    這篇文章主要介紹了vue實現(xiàn)自定義樹形組件的示例代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • vue3實現(xiàn)監(jiān)聽store中state狀態(tài)變化的簡單方法

    vue3實現(xiàn)監(jiān)聽store中state狀態(tài)變化的簡單方法

    這篇文章主要給大家介紹了關(guān)于vue3實現(xiàn)監(jiān)聽store中state狀態(tài)變化的簡單方法,store是一個狀態(tài)管理工具,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • vant如何實現(xiàn)Collapse折疊面板標(biāo)題自定義

    vant如何實現(xiàn)Collapse折疊面板標(biāo)題自定義

    這篇文章主要介紹了vant如何實現(xiàn)Collapse折疊面板標(biāo)題自定義,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue如何批量引入組件、注冊和使用詳解

    vue如何批量引入組件、注冊和使用詳解

    這篇文章主要給大家介紹了關(guān)于vue如何批量引入組件、注冊和使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05

最新評論

丰镇市| 通化市| 新宾| 共和县| 钦州市| 南漳县| 信阳市| 四会市| 普格县| 兴国县| 防城港市| 保山市| 定边县| 宁都县| 宁蒗| 临澧县| 昭通市| 兴和县| 平南县| 泸溪县| 天等县| 桃源县| 青河县| 怀集县| 洛隆县| 广宁县| 大庆市| 湘潭市| 海兴县| 临高县| 辽宁省| 乐山市| 万盛区| 洛川县| 兰溪市| 宣威市| 突泉县| 酉阳| 青铜峡市| 台中县| 北辰区|