hookehuyr

✨ feat(动态路由): 重写动态路由模块,解决子路由无法使用和刷新白屏问题

...@@ -8,7 +8,18 @@ const routes = [{ ...@@ -8,7 +8,18 @@ const routes = [{
8 title: 'html转图片', 8 title: 'html转图片',
9 name: '' 9 name: ''
10 }, 10 },
11 - children: [] 11 + children: [{
12 + path: 'children',
13 + redirect: '',
14 + name: 'html转图片',
15 + component: 'children-test',
16 + keepAlive: '',
17 + meta: {
18 + title: 'html转图片',
19 + name: ''
20 + }
21 + }]
12 }] 22 }]
23 +// const routes = []
13 24
14 export default routes 25 export default routes
......
1 /* 1 /*
2 * @Date: 2022-05-26 13:57:28 2 * @Date: 2022-05-26 13:57:28
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2022-06-15 11:00:05 4 + * @LastEditTime: 2022-06-29 17:11:27
5 * @FilePath: /tswj/src/router.js 5 * @FilePath: /tswj/src/router.js
6 * @Description: 文件描述 6 * @Description: 文件描述
7 */ 7 */
8 import { createRouter, createWebHashHistory } from 'vue-router'; 8 import { createRouter, createWebHashHistory } from 'vue-router';
9 import RootRoute from './route.js'; 9 import RootRoute from './route.js';
10 -import { generateRoute } from './utils/generateRoute' 10 +import asyncRoutesArr from "./mock/routes"
11 +import generateRoutes from './utils/generateRoute'
11 12
12 // TAG: 路由配置表 13 // TAG: 路由配置表
13 /** 14 /**
...@@ -35,11 +36,19 @@ const router = createRouter({ ...@@ -35,11 +36,19 @@ const router = createRouter({
35 /** 36 /**
36 * generateRoute 负责把后台返回数据拼接成项目需要的路由结构,动态添加到路由表里面 37 * generateRoute 负责把后台返回数据拼接成项目需要的路由结构,动态添加到路由表里面
37 */ 38 */
38 -router.beforeEach((to) => { // next 不能用了,被return替代了 39 +router.beforeEach((to, from, next) => {
39 - if (!router.hasRoute(to.name)) { // 如果不存在 40 + if (!router.hasRoute(to.name)) { // 如果路由地址不存在
40 - router.addRoute(generateRoute(to)) // 新增路由 41 + // 模拟异步操作
41 - // 触发重定向 42 + setTimeout(() => {
42 - return to.fullPath 43 + if (!asyncRoutesArr.length) return; // 没有动态路由避免报错
44 + const arr = generateRoutes(asyncRoutesArr); // 在路由守卫处生成,避免有子路由时刷新白屏问题。
45 + arr.forEach(item => {
46 + router.addRoute(item) // 新增路由
47 + })
48 + next({ ...to, replace: true })
49 + }, 1000);
50 + } else {
51 + next()
43 } 52 }
44 }) 53 })
45 54
......
1 /* 1 /*
2 * @Date: 2022-05-16 17:21:45 2 * @Date: 2022-05-16 17:21:45
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2022-06-29 16:00:23 4 + * @LastEditTime: 2022-06-29 17:00:15
5 * @FilePath: /tswj/src/utils/generateRoute.js 5 * @FilePath: /tswj/src/utils/generateRoute.js
6 * @Description: 文件描述 6 * @Description: 文件描述
7 */ 7 */
8 -import asyncRoutes from "../mock/routes"
9 8
10 /** 9 /**
11 * 根据后台返回的路径,生成页面的组件模版 10 * 根据后台返回的路径,生成页面的组件模版
...@@ -16,14 +15,12 @@ function loadView(component) { ...@@ -16,14 +15,12 @@ function loadView(component) {
16 return () => import(`../views/${component}.vue`) 15 return () => import(`../views/${component}.vue`)
17 } 16 }
18 17
19 -const formatRoutesArr = []
20 -
21 /** 18 /**
22 * 生成路由结构 19 * 生成路由结构
23 - * @param {*} routes 动态获取数据 20 + * @param {*} routes
24 - * @description 动态路由没有嵌套结构,children被拉平,都是平级的结构
25 */ 21 */
26 -const formatRoutes = (routes) => { 22 +const generateRoutes = (routes) => {
23 + const arr = []
27 routes.forEach(route => { 24 routes.forEach(route => {
28 const router = {} 25 const router = {}
29 const { 26 const {
...@@ -42,25 +39,10 @@ const formatRoutes = (routes) => { ...@@ -42,25 +39,10 @@ const formatRoutes = (routes) => {
42 router.component = loadView(component) 39 router.component = loadView(component)
43 keepAlive && (router.keepAlive = keepAlive) 40 keepAlive && (router.keepAlive = keepAlive)
44 meta && (router.meta = meta) 41 meta && (router.meta = meta)
45 - router.children = !Array.isArray(children) || formatRoutes(children); 42 + router.children = !Array.isArray(children) || generateRoutes(children);
46 - 43 + arr.push(router)
47 - formatRoutesArr.push(router)
48 }) 44 })
45 + return arr
49 } 46 }
50 47
51 -formatRoutes(asyncRoutes) 48 +export default generateRoutes;
52 -
53 -/**
54 - * 生成动态路由
55 - * @param {*} to
56 - * @returns 符合路径条件的动态路由结构
57 - */
58 -export const generateRoute = (to) => {
59 - let router = ''
60 - formatRoutesArr.forEach(item => {
61 - if (item.path === to.path) {
62 - router = item
63 - }
64 - })
65 - return { ...to, ...router }
66 -}
......
1 +<!--
2 + * @Date: 2022-06-29 15:57:45
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2022-06-29 15:57:52
5 + * @FilePath: /tswj/src/views/children-test.vue
6 + * @Description: 文件描述
7 +-->
8 +<template>
9 + <div class="">test</div>
10 +</template>
11 +
12 +<script setup>
13 +import { ref } from 'vue'
14 +import { useRoute, useRouter } from 'vue-router'
15 +
16 +import { Cookies, $, _, axios, storeToRefs, mainStore, Toast, useTitle } from '@/utils/generatePackage.js'
17 +//import { } from '@/utils/generateModules.js'
18 +//import { } from '@/utils/generateIcons.js'
19 +//import { } from '@/composables'
20 +const $route = useRoute();
21 +const $router = useRouter();
22 +useTitle($route.meta.title);
23 +
24 +</script>
25 +
26 +<style lang="less" scoped>
27 +
28 +</style>
...@@ -5,11 +5,13 @@ ...@@ -5,11 +5,13 @@
5 <img :src="logo_image" alt="" /> 5 <img :src="logo_image" alt="" />
6 </div> 6 </div>
7 <div v-if="imgUrl"> 7 <div v-if="imgUrl">
8 - <img :src="imgUrl" alt="" crossOrigin="anonymous" style="width: 300px; height: 300px;"/> 8 + <img :src="imgUrl" alt="" crossOrigin="anonymous" style="width: 300px; height: 300px;" />
9 </div> 9 </div>
10 <div> 10 <div>
11 <div @click="createImage">合成图片</div> 11 <div @click="createImage">合成图片</div>
12 + <div @click="btn">child</div>
12 </div> 13 </div>
14 + <router-view></router-view>
13 </div> 15 </div>
14 </template> 16 </template>
15 17
...@@ -67,6 +69,9 @@ export default { ...@@ -67,6 +69,9 @@ export default {
67 console.log("绘制失败"); 69 console.log("绘制失败");
68 }); 70 });
69 }); 71 });
72 + },
73 + btn () {
74 + this.$router.push('/image/children')
70 } 75 }
71 } 76 }
72 } 77 }
...@@ -106,4 +111,4 @@ img { ...@@ -106,4 +111,4 @@ img {
106 .m-btn { 111 .m-btn {
107 margin-left: 50px; 112 margin-left: 50px;
108 } 113 }
109 -</style>
...\ No newline at end of file ...\ No newline at end of file
114 +</style>
......