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

vue3實(shí)現(xiàn)動(dòng)態(tài)添加路由

 更新時(shí)間:2024年04月25日 11:14:59   作者:m0_74019046  
這篇文章主要介紹了vue3實(shí)現(xiàn)動(dòng)態(tài)添加路由方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

動(dòng)態(tài)路由的設(shè)置

1.登入后獲取后端返回的路由,并存入localStorage中

  const menu = [
        {
          id: "1",
          name: "系統(tǒng)總攬",
          type: 1,
          url: "/main/1",
          icon: "el-icon-ship",
          children: [
            { id: "11", name: "1111", type: 1, url: "/main/first/1", icon: "" },
            { id: "12", name: "33", type: 1, url: "/main/first/2", icon: "" }
          ]
        },

        {
          id: "2",
          name: "系統(tǒng)管理",
          type: 1,
          url: "/main/4",
          icon: "el-icon-moon",
          children: [
            { id: "21", name: "系統(tǒng)xx", type: 1, url: "/main/two/1", icon: "" },
            { id: "22", name: "系統(tǒng)11", type: 1, url: "/main/two/2", icon: "" }
          ]
        },

        {
          id: "3",
          name: "商品中心",
          type: 1,
          url: "/main/7",
          icon: "el-icon-lightning",
          children: [
            {
              id: "31",
              name: "系統(tǒng)zz",
              type: 1,
              url: "/main/three/1",
              icon: ""
            },
            {
              id: "32",
              name: "系統(tǒng)cc",
              type: 1,
              url: "/main/three/2",
              icon: ""
            }
          ]
        },
        {
          id: "8",
          name: "ad",
          type: 1,
          url: "/main/8",
          icon: "el-icon-goods",
          children: [
            { id: "9", name: "qqq", type: 1, url: "/main/four/1", icon: "" },
            { id: "10", name: "打算", type: 1, url: "/main/four/2", icon: "" }
          ]
        }
      ]
       // 存入pinia中
       this.userMenu = menu
       //封裝的localStorage
      localCache.setCache("userMenu", menu)

2.獲取本地的路由文件

   function getlocalRoute(){
       //routerRoute本地路由
      const routerRoute: RouteRecordRaw[] = []
      //  1.獲取 router/xx/xx.ts文件   import.meta.glob() vite提供的
      const route: Record<string, any> = import.meta.glob(
        "../../router/**/*.ts",
        { eager: true }
      )
     //2.把所有的路由添加到routerRoute中
      for (const key in route) {
        const module = route[key]
        console.log(module)
        routerRoute.push(module.default)
      }
      return    routerRoute
  }

通過 import.meta.glob() 導(dǎo)入的路由

default就是路由對象

routerRoute:

3. 添加路由

  let isFirst = true
  function (userMenu:any){
      //本地路由
      const localRoute = getlocalRoute()
      const routes:RouteRecordRaw[] = []
      // 匹配路由
      for(const menu of userMenu){
         for(const subMenu of menu.children ){
         //匹配到的子路由
         //route的結(jié)構(gòu):{path: '/main/first/1', component: ?}
           const route =  localRoute.find((item) => item.path == subMenu.url)
           // 記錄第一個(gè)路由,進(jìn)入主頁后會(huì)跳轉(zhuǎn)到這個(gè)路由
           if(route && isFirst){
              isFirst = false
              // 封裝的  localStorage
            localCache.setCache("first", myRoute)
           }
           if(route){
              //給1級路由重定向到它的第一個(gè)子路由(只需要添加1次)
              //如果 routerRoute里沒有加過1級路由的話就需要添加1級路由,并重定向到他的第一個(gè)路由
              if(!routes.find((item) => item.path == menu.url)){
              // 這里的redirect:不能是 menu.children[0],有可能它沒有第一個(gè)子路由的權(quán)限
               routes.push({ path:menu.url, redirect: route .path })
              }
              // 添加路由
              routes.push(route)
           }
        }
     }
     return   routes
  } 
 //最終再添加到main下:
   router.addRoute("main", routes)

因?yàn)楂@取本地的路由文件刷新后會(huì)消失,在pinia中設(shè)置一個(gè)方法用來在頁面刷新后重新加載路由loadLocal 具體操作和上面添加路由的方式相同。

刷新頁面后,會(huì)重新加載 pinia, 在pinia加載完后再加載本地?cái)?shù)據(jù),添加路由

import { createPinia } from "pinia"
import useLoginStore from "./login/index"
import type { App } from "vue"
const pinia = createPinia()
function store(app: App<Element>) {
  //pinia加載完后才能使用store里面state、action和getters
  app.use(pinia)
  //pinia加載完后 加載本地?cái)?shù)據(jù),添加路由
  const login = useLoginStore()
  login.loadLocal()
}
export default store

面包屑的使用

<div class="home">
    <el-breadcrumb :separator-icon="ArrowRight">
      <template v-for="item in menuList" :key="item.name">
        <el-breadcrumb-item :to="item.path">{{ item.name }}</el-breadcrumb-item>
      </template>
    </el-breadcrumb>
  </div>
<script setup lang="ts">
  const store = useLoginStore()
  const route = useRoute()
  const fun = () => {
  const list: any[] = []
  for (let menu of store.userMenu) {
    for (let child of menu.children) {
      //   獲取面包屑路由,并添加路由
      //當(dāng)前點(diǎn)擊的路由== 子路由
      if (child.url == route.path) {
        console.log(child.url)
        // 1級路由,之前注冊時(shí)設(shè)置1級路由會(huì)重定向到它的第一個(gè)子路由
        list.push({ name: menu.name, path: menu.url })
        //子路由 當(dāng)前點(diǎn)擊的路由
        list.push({ name: child.name, path: child.url })
      }
    }
  }
  //list [1級路由,子路由]
  return list
}
// 當(dāng)點(diǎn)擊的路由變化時(shí)會(huì)匹配新的面包屑的路由
const menuList = computed(() => {
  return fun()
})
</script>

路由的高亮

 <div class="main-menu">
    <el-row class="tac">
      <el-col>
        <el-menu
          :default-active="defaultActive"
          class="el-menu-vertical-demo"
          :unique-opened="true"
        >
          <template v-for="item in menu" :key="item.id">
            <el-sub-menu :index="item.id + ''">
              <template #title>
                <el-icon
                  ><component :is="item.icon.split('-icon-')[1]"
                /></el-icon>
                <span>{{ item.name }}</span>
              </template>
              <template v-for="child in item.children" :key="child.id">
                <el-menu-item-group>
                  <el-menu-item :index="child.id" @click="cli(child)">{{
                    child.name
                  }}</el-menu-item>
                </el-menu-item-group>
              </template>
            </el-sub-menu>
          </template>
        </el-menu>
      </el-col>
    </el-row>
  </div>
//拿到所有路由
const loginStore = useloginStore()
const menu = loginStore.userMenu
//當(dāng)前進(jìn)入頁面的路由
const route = useRoute()
// 點(diǎn)擊對應(yīng)的菜單或輸入路徑后,對應(yīng)的路由要高亮
const active = () => {
  for (const item of loginStore.userMenu) {
    console.log(route.path)
    for (const child of item.children) {
      //子路由 == 用戶輸入的路由
      if (child.url === route.path) {
        console.log(child.id)
        return child.id + ""
      }
    }
  }
  // 返回的默認(rèn)路由
  return "11"
}

let defaultActive = computed(() => {
  const defaults = active()
  return defaults
})

封裝模塊

配置項(xiàng)

const searchconfig = {
  formItems: [
    { type: "input", prop: "name", label: "部門名稱", placeholder: "xxx" },
    { type: "date-picker", prop: "date", label: "時(shí)間", placeholder: "xxx" },
    { type: "input", prop: "leader", label: "領(lǐng)導(dǎo)", placeholder: "xxx" },
    {
      type: "select",
      prop: "selects",
      label: "選擇",
      placeholder: "xxx",
      options: [
        { label: "1", value: 1 },
        { label: "2", value: 2 }
      ]
    }
  ]
}
export default searchconfig

遍歷配置項(xiàng)

  <div class="home">;;
    <div>
      <pageSerach @search="cli" :searchConfig="searchRef"></pageSerach>
    </div>
    <div>
      <pageContent :contentConfig="contentconfig">
        <template #name="scope">
          <span>xxx:{{ scope.row[scope.prop] }}</span>
        </template>
        <template #id="scope">
          <span>xxx:{{ scope.row[scope.prop] }}</span>
        </template>
      </pageContent>
    </div>
    <div>
      <pageBottom></pageBottom>
    </div>
  </div>

如果有些配置是從后端傳入的,例如option的value,可以這樣添加:

const searchRef = computed(() => {
  // 從后端獲取option的值,再給 searchconfig 里的option添加值
  console.log("xx")
  searchconfig.formItems.forEach((item) => {
    if (item.prop == "selects") {
      item?.options?.push({ label: "3", value: 3 })
    }
  })
  return searchconfig
})

總結(jié)

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

相關(guān)文章

  • vue3.0-props、computed、自定義事件方式

    vue3.0-props、computed、自定義事件方式

    這篇文章主要介紹了vue3.0-props、computed、自定義事件方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • v-distpicker地區(qū)選擇器組件使用實(shí)例詳解

    v-distpicker地區(qū)選擇器組件使用實(shí)例詳解

    代碼添加了一個(gè)vDistpickerHandle的事件處理函數(shù)對地區(qū)選擇器中的數(shù)據(jù)進(jìn)行處理,將數(shù)據(jù)存儲(chǔ)到form對象的相應(yīng)屬性中,方便數(shù)據(jù)提交,這篇文章主要介紹了v-distpicker地區(qū)選擇器組件使用,需要的朋友可以參考下
    2024-02-02
  • 使用el-upload實(shí)現(xiàn)文件上傳方式(包括預(yù)覽,下載)

    使用el-upload實(shí)現(xiàn)文件上傳方式(包括預(yù)覽,下載)

    該文章介紹了兩種文件上傳、預(yù)覽和下載的方法,分別是通過卡片形式和按鈕形式,卡片形式中的下載和預(yù)覽功能尚未完善,父組件使用了el-upload組件,可以實(shí)現(xiàn)新增、編輯和查看文件的功能
    2025-11-11
  • vue通過v-show實(shí)現(xiàn)回到頂部top效果

    vue通過v-show實(shí)現(xiàn)回到頂部top效果

    這篇文章主要介紹了vue通過v-show實(shí)現(xiàn)回到頂部top效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • Vue3使用vuedraggable實(shí)現(xiàn)拖拽排序功能實(shí)例代碼

    Vue3使用vuedraggable實(shí)現(xiàn)拖拽排序功能實(shí)例代碼

    vuedraggable是一個(gè)基于Sortable.js的Vue.js拖拽排序插件,提供簡單、靈活且強(qiáng)大的拖拽功能,支持Vue2和Vue3,這篇文章主要介紹了Vue3使用vuedraggable實(shí)現(xiàn)拖拽排序功能的相關(guān)資料,需要的朋友可以參考下
    2026-02-02
  • Vue用Export2Excel導(dǎo)出excel,多級表頭數(shù)據(jù)方式

    Vue用Export2Excel導(dǎo)出excel,多級表頭數(shù)據(jù)方式

    這篇文章主要介紹了Vue用Export2Excel導(dǎo)出excel,多級表頭數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Vue代理報(bào)錯(cuò)404問題及解決(vue配置proxy)

    Vue代理報(bào)錯(cuò)404問題及解決(vue配置proxy)

    這篇文章主要介紹了Vue代理報(bào)錯(cuò)404問題及解決(vue配置proxy),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • vue動(dòng)態(tài)設(shè)置img的src路徑實(shí)例

    vue動(dòng)態(tài)設(shè)置img的src路徑實(shí)例

    今天小編就為大家分享一篇vue動(dòng)態(tài)設(shè)置img的src路徑實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue多組件倉庫開發(fā)與發(fā)布詳解

    Vue多組件倉庫開發(fā)與發(fā)布詳解

    這篇文章主要介紹了Vue多組件倉庫開發(fā)與發(fā)布詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-02-02
  • 詳解vue-router2.0動(dòng)態(tài)路由獲取參數(shù)

    詳解vue-router2.0動(dòng)態(tài)路由獲取參數(shù)

    本篇文章主要介紹了詳解vue-router2.0動(dòng)態(tài)路由獲取參數(shù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06

最新評論

乌兰浩特市| 新巴尔虎左旗| 高青县| 恩平市| 建宁县| 遂川县| 阳朔县| 荃湾区| 开江县| 诸暨市| 台山市| 白城市| 顺平县| 云浮市| 保山市| 会昌县| 乳源| 临桂县| 兴化市| 桃园市| 玛纳斯县| 绍兴市| 延安市| 庆安县| 芦山县| 东辽县| 保山市| 锦州市| 新蔡县| 盱眙县| 东莞市| 分宜县| 宣汉县| 青海省| 桂阳县| 水城县| 麦盖提县| 凤冈县| 灵武市| 沾化县| 东港市|