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

vue3.0路由自動(dòng)導(dǎo)入的方法實(shí)例

 更新時(shí)間:2021年04月09日 09:49:02   作者:放風(fēng)嘍  
這篇文章主要給大家介紹了關(guān)于vue3.0路由自動(dòng)導(dǎo)入的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、前提

我們使用的是require.context方法導(dǎo)入,在vite創(chuàng)建的項(xiàng)目?jī)?nèi)使用會(huì)報(bào)錯(cuò)"require not found",所以必須用webpack創(chuàng)建項(xiàng)目。或者有大能可以說(shuō)說(shuō)vite怎么解決這個(gè)問(wèn)題。

二、規(guī)則

我們使用的規(guī)則是,搜索src/views/路徑下的所有目錄和子目錄,搜索文件名叫做"index.vue"的文件,使用上級(jí)目錄的名字作為組件名,進(jìn)行注冊(cè)。結(jié)構(gòu)如下:

和公共組件注冊(cè)一樣,我們只注冊(cè)index.vue組件,其他名稱的組件不進(jìn)行注冊(cè)。

三、導(dǎo)入

// src/router/index.ts

import {createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw} from 'vue-router'
import store from "@/store";
import daxie from "@/util/upper";		// 引入一個(gè)方法,將字符串的首字母進(jìn)行大寫(xiě),我習(xí)慣將pathname首字母大寫(xiě)

// 路由自動(dòng)化注冊(cè)
const routerList:any = []
const requireComponent = require.context('@/views', true, /index.vue$/) // 找到views路徑下的所有文件
const dynamic_route = requireComponent.keys().filter(fileName => {
    return true
})
// 現(xiàn)在文件數(shù)組是亂序的,我們首先進(jìn)行排序,父級(jí)路由在前面,如果是子級(jí)路由在前面,結(jié)果父級(jí)理由還沒(méi)有創(chuàng)建,就會(huì)報(bào)錯(cuò)
// console.log(dynamic_route,"排序前")
dynamic_route.sort(function (a,b):number{
    const jieguoa:any = a.split("").filter((item:string)=>{
        return "/" == item
    })
    const jieguob:any = b.split("").filter((item:string)=>{
        return "/" == item
    })
    if(jieguoa.length<jieguob.length){return -1}
    if(jieguoa.length>jieguob.length){return 1}
    return 0
})

// console.log(dynamic_route,"排序后")


dynamic_route.forEach(fileName => {
    const path = fileName.replace(".", "")
    const namelist = fileName.replace(".", "").replace("index.vue", "").split("/").filter((i:any) => {
        return i
    })
    // 測(cè)試配置
    const componentConfig = requireComponent(fileName)
    // 組件可以隨意添加任何屬性,目前添加一個(gè)canshu屬性,是一個(gè)數(shù)組,里面存放著需要的動(dòng)態(tài)參數(shù)
    // console.log(componentConfig.default,"組件配置2")
    // 每一層都需要手動(dòng)指定,只做三層吧
    if(namelist.length == 1){
        routerList.push({
            path:"/"+ namelist[namelist.length - 1],
            name: daxie(namelist[namelist.length-1]),
            component:()=>import(`../views${path}`),
            children:[],
        })
    }else if(namelist.length == 2){
        routerList.forEach((item:any)=>{
            if(item.name == daxie(namelist[0])){
                // 判斷組件是否需要參數(shù)
                const canshu = componentConfig.default.canshu || []
                if(canshu){
                    for (let i=0;i<canshu.length;i++){
                        canshu[i] = "/:"+canshu[i]
                    }
                    item.children.push({
                        path: namelist[namelist.length-1] + canshu.join(""),
                        name: daxie(namelist[namelist.length-1]),
                        component:()=>import(`../views${path}`),
                        children:[],
                    })
                }else{
                    item.children.push({
                        path: namelist[namelist.length-1],
                        name: daxie(namelist[namelist.length-1]),
                        component:()=>import(`../views${path}`),
                        children:[],
                    })
                }
            }
        })
    }else if(namelist.length == 3){
        routerList.forEach((item:any)=>{
            if(item.name == daxie(namelist[0])){
                item.children.forEach((yuansu:any)=>{
                    if(yuansu.name == daxie(namelist[1])){
                        // 判斷是不是需要參數(shù)
                        const canshu = componentConfig.default.canshu || []
                        if(canshu){
                            for (let i=0;i<canshu.length;i++){
                                canshu[i] = "/:"+canshu[i]
                            }
                            yuansu.children.push({
                                path: namelist[namelist.length - 1]+canshu.join(""),
                                name: daxie(namelist[namelist.length-1]),
                                component:()=>import(`../views${path}`),
                            })
                        }else {
                            yuansu.children.push({
                                path: namelist[namelist.length - 1],
                                name: daxie(namelist[namelist.length-1]),
                                component:()=>import(`../views${path}`),
                            })
                        }
                    }
                })
            }
        })
    }
})
const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Home',
        component: ()=>import("@/views/shouye/shouye.vue")
    },
    {
        path: '/about',
        name: 'About',
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    },
    ...routerList,
    {
        path: '/404',
        name: '404',
        component: () => import('@/views/404.vue')
    },
    {
        path: '/:pathMatch(.*)',
        redirect: '/404'
    },
]
// 注意順序,根據(jù)最新的路由匹配規(guī)則,404頁(yè)面必須在最后,
console.log(routes,"查看路由表內(nèi)容")

const router = createRouter({
    history: createWebHistory(),
    // history: createWebHashHistory(),
    routes
})

export default router

這樣,只需要根據(jù)規(guī)則創(chuàng)建組件,就會(huì)被自動(dòng)注冊(cè)到路由里面。免去手動(dòng)注冊(cè)的繁瑣操作。

總結(jié)

到此這篇關(guān)于vue3.0路由自動(dòng)導(dǎo)入的文章就介紹到這了,更多相關(guān)vue3.0路由自動(dòng)導(dǎo)入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue實(shí)現(xiàn)模糊查詢filter()實(shí)例詳解

    Vue實(shí)現(xiàn)模糊查詢filter()實(shí)例詳解

    因?yàn)榻赵趯W(xué)習(xí)并使用VUE,客戶有一個(gè)要求,要輸入框可模糊查詢并帶有下拉提示的應(yīng)用,數(shù)據(jù)從接口取,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)模糊查詢filter()的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • Vue項(xiàng)目服務(wù)器部署刷新頁(yè)面404問(wèn)題及解決

    Vue項(xiàng)目服務(wù)器部署刷新頁(yè)面404問(wèn)題及解決

    這篇文章主要介紹了Vue項(xiàng)目服務(wù)器部署刷新頁(yè)面404問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue 使用async寫(xiě)數(shù)字動(dòng)態(tài)加載效果案例

    vue 使用async寫(xiě)數(shù)字動(dòng)態(tài)加載效果案例

    這篇文章主要介紹了vue 使用async寫(xiě)數(shù)字動(dòng)態(tài)加載效果案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Vue3系列教程之插槽slot詳解

    Vue3系列教程之插槽slot詳解

    插槽(slot)可以說(shuō)在一個(gè)?Vue?項(xiàng)目里面處處都有它的身影,比如我們使用一些UI?組件庫(kù)的時(shí)候,我們通??梢允褂貌宀蹃?lái)自定義我們的內(nèi)容,今天通過(guò)本文給大家介紹vue3插槽slot的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2022-08-08
  • Vue 構(gòu)造選項(xiàng) - 進(jìn)階使用說(shuō)明

    Vue 構(gòu)造選項(xiàng) - 進(jìn)階使用說(shuō)明

    這篇文章主要介紹了Vue 構(gòu)造選項(xiàng) - 進(jìn)階使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • Vue列表頁(yè)渲染優(yōu)化詳解

    Vue列表頁(yè)渲染優(yōu)化詳解

    這篇文章主要為大家詳細(xì)介紹了Vue列表頁(yè)渲染優(yōu)化的操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • vue3如何獲取綁定頁(yè)面dom元素

    vue3如何獲取綁定頁(yè)面dom元素

    這篇文章主要介紹了vue3如何獲取綁定頁(yè)面dom元素問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Vue前端導(dǎo)出Excel文件的詳細(xì)實(shí)現(xiàn)方案

    Vue前端導(dǎo)出Excel文件的詳細(xì)實(shí)現(xiàn)方案

    在開(kāi)發(fā)后臺(tái)管理系統(tǒng)的時(shí)候,很多地方都要用到導(dǎo)出excel表格,比如將table中的數(shù)據(jù)導(dǎo)出到本地,下面這篇文章主要給大家介紹了關(guān)于Vue導(dǎo)出Excel文件的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • Vue使用swiper問(wèn)題(5.2.0版本,避免踩坑)

    Vue使用swiper問(wèn)題(5.2.0版本,避免踩坑)

    這篇文章主要介紹了Vue使用swiper問(wèn)題(5.2.0版本,避免踩坑),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Vue+Elementui el-tree樹(shù)只能選擇子節(jié)點(diǎn)并且支持檢索功能

    Vue+Elementui el-tree樹(shù)只能選擇子節(jié)點(diǎn)并且支持檢索功能

    這篇文章給大家介紹了Vue+Element UI el-tree樹(shù)只能選擇子節(jié)點(diǎn)并且支持檢索的文章,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-12-12

最新評(píng)論

运城市| 区。| 和平区| 土默特右旗| 富顺县| 云浮市| 竹北市| 高陵县| 宿州市| 辽中县| 宁夏| 龙泉市| 嘉荫县| 中卫市| 宁远县| 阳东县| 磐石市| 临漳县| 苍溪县| 四子王旗| 中卫市| 康马县| 宿松县| 宜良县| 阿荣旗| 渝中区| 西华县| 蒙城县| 会理县| 延边| 喜德县| 赤峰市| 宜阳县| 阿鲁科尔沁旗| 茶陵县| 合肥市| 高台县| 景东| 潮安县| 富平县| 伽师县|