hookehuyr

✨ feat(动态路由模块): 修复路由控制台报错问题,处理动态路由传值问题

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-29 17:11:27 4 + * @LastEditTime: 2022-06-29 20:06:11
5 * @FilePath: /tswj/src/router.js 5 * @FilePath: /tswj/src/router.js
6 * @Description: 文件描述 6 * @Description: 文件描述
7 */ 7 */
...@@ -25,8 +25,6 @@ Object.keys(modules).forEach((key) => { ...@@ -25,8 +25,6 @@ Object.keys(modules).forEach((key) => {
25 }); 25 });
26 26
27 // 创建路由实例并传递 `routes` 配置 27 // 创建路由实例并传递 `routes` 配置
28 -// 你可以在这里输入更多的配置,但我们在这里
29 -
30 const router = createRouter({ 28 const router = createRouter({
31 history: createWebHashHistory('/index.html'), 29 history: createWebHashHistory('/index.html'),
32 routes: [...RootRoute, ...routeModuleList] 30 routes: [...RootRoute, ...routeModuleList]
...@@ -37,7 +35,8 @@ const router = createRouter({ ...@@ -37,7 +35,8 @@ const router = createRouter({
37 * generateRoute 负责把后台返回数据拼接成项目需要的路由结构,动态添加到路由表里面 35 * generateRoute 负责把后台返回数据拼接成项目需要的路由结构,动态添加到路由表里面
38 */ 36 */
39 router.beforeEach((to, from, next) => { 37 router.beforeEach((to, from, next) => {
40 - if (!router.hasRoute(to.name)) { // 如果路由地址不存在 38 + // 使用404为中转页面,避免动态路由没有渲染出来,控制台报错问题
39 + if (to.path == '/404' && to.redirectedFrom != undefined) {
41 // 模拟异步操作 40 // 模拟异步操作
42 setTimeout(() => { 41 setTimeout(() => {
43 if (!asyncRoutesArr.length) return; // 没有动态路由避免报错 42 if (!asyncRoutesArr.length) return; // 没有动态路由避免报错
...@@ -45,7 +44,8 @@ router.beforeEach((to, from, next) => { ...@@ -45,7 +44,8 @@ router.beforeEach((to, from, next) => {
45 arr.forEach(item => { 44 arr.forEach(item => {
46 router.addRoute(item) // 新增路由 45 router.addRoute(item) // 新增路由
47 }) 46 })
48 - next({ ...to, replace: true }) 47 + // 重写被404覆盖路由信息
48 + next({ ...to.redirectedFrom, replace: true });
49 }, 1000); 49 }, 1000);
50 } else { 50 } else {
51 next() 51 next()
......
1 +/*
2 + * @Date: 2022-06-15 17:09:03
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2022-06-29 18:36:37
5 + * @FilePath: /tswj/src/router/routes/modules/test/index.js
6 + * @Description: 文件描述
7 + */
1 const index = [{ 8 const index = [{
2 path: '/html2canvas', 9 path: '/html2canvas',
3 name: 'html2canvas', 10 name: 'html2canvas',
...@@ -18,6 +25,20 @@ const index = [{ ...@@ -18,6 +25,20 @@ const index = [{
18 title: '测试slot插槽', 25 title: '测试slot插槽',
19 }, 26 },
20 children: [], 27 children: [],
28 +},
29 +{
30 + path: '/:pathMatch(.*)*',
31 + redirect: '/404'
32 +},
33 +{
34 + path: '/404',
35 + name: '404',
36 + component: () => import('@/views/test/404.vue'),
37 + meta: {
38 + icon: '',
39 + title: '404',
40 + },
41 + children: [],
21 }]; 42 }];
22 43
23 export default index; 44 export default index;
......
...@@ -71,7 +71,12 @@ export default { ...@@ -71,7 +71,12 @@ export default {
71 }); 71 });
72 }, 72 },
73 btn () { 73 btn () {
74 - this.$router.push('/image/children') 74 + this.$router.push({
75 + path: '/image/children',
76 + query: {
77 + abc: 1
78 + }
79 + })
75 } 80 }
76 } 81 }
77 } 82 }
......