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

vue3實現動態(tài)添加路由

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

動態(tài)路由的設置

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() 導入的路由

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的結構:{path: '/main/first/1', component: ?}
           const route =  localRoute.find((item) => item.path == subMenu.url)
           // 記錄第一個路由,進入主頁后會跳轉到這個路由
           if(route && isFirst){
              isFirst = false
              // 封裝的  localStorage
            localCache.setCache("first", myRoute)
           }
           if(route){
              //給1級路由重定向到它的第一個子路由(只需要添加1次)
              //如果 routerRoute里沒有加過1級路由的話就需要添加1級路由,并重定向到他的第一個路由
              if(!routes.find((item) => item.path == menu.url)){
              // 這里的redirect:不能是 menu.children[0],有可能它沒有第一個子路由的權限
               routes.push({ path:menu.url, redirect: route .path })
              }
              // 添加路由
              routes.push(route)
           }
        }
     }
     return   routes
  } 
 //最終再添加到main下:
   router.addRoute("main", routes)

因為獲取本地的路由文件刷新后會消失,在pinia中設置一個方法用來在頁面刷新后重新加載路由loadLocal 具體操作和上面添加路由的方式相同。

刷新頁面后,會重新加載 pinia, 在pinia加載完后再加載本地數據,添加路由

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加載完后 加載本地數據,添加路由
  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) {
      //   獲取面包屑路由,并添加路由
      //當前點擊的路由== 子路由
      if (child.url == route.path) {
        console.log(child.url)
        // 1級路由,之前注冊時設置1級路由會重定向到它的第一個子路由
        list.push({ name: menu.name, path: menu.url })
        //子路由 當前點擊的路由
        list.push({ name: child.name, path: child.url })
      }
    }
  }
  //list [1級路由,子路由]
  return list
}
// 當點擊的路由變化時會匹配新的面包屑的路由
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
//當前進入頁面的路由
const route = useRoute()
// 點擊對應的菜單或輸入路徑后,對應的路由要高亮
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 + ""
      }
    }
  }
  // 返回的默認路由
  return "11"
}

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

封裝模塊

配置項

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

遍歷配置項

  <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
})

總結

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

相關文章

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

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

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

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

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

    使用el-upload實現文件上傳方式(包括預覽,下載)

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

    vue通過v-show實現回到頂部top效果

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

    Vue3使用vuedraggable實現拖拽排序功能實例代碼

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

    Vue用Export2Excel導出excel,多級表頭數據方式

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

    Vue代理報錯404問題及解決(vue配置proxy)

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

    vue動態(tài)設置img的src路徑實例

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

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

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

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

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

最新評論

伊金霍洛旗| 宜良县| 温宿县| 建水县| 南通市| 宁海县| 资中县| 宁乡县| 阳春市| 钟祥市| 石台县| 铜山县| 西城区| 阳高县| 长白| 大同市| 新营市| 金华市| 河津市| 买车| 那曲县| 香港| 华宁县| 沁阳市| 青冈县| 天气| 岳阳市| 西乌珠穆沁旗| 项城市| 华容县| 本溪市| 扶绥县| 眉山市| 阿拉善左旗| 行唐县| 象州县| 井陉县| 彭泽县| 吉木萨尔县| 乌兰县| 施甸县|