hookehuyr

配置动态路由功能

1 +const routes = [{
2 + path: '/image',
3 + redirect: '',
4 + name: 'html转图片',
5 + component: 'html2canvas',
6 + keepAlive: '',
7 + meta: {
8 + title: 'html转图片',
9 + name: ''
10 + },
11 + children: []
12 +}]
13 +
14 +export default routes
1 import { createRouter, createWebHashHistory } from 'vue-router'; 1 import { createRouter, createWebHashHistory } from 'vue-router';
2 import routes from './route.js'; 2 import routes from './route.js';
3 +import { generateRoute } from './utils/generateRoute'
3 4
4 // TODO: 微信分享测试 5 // TODO: 微信分享测试
5 import share from '@/utils/share' 6 import share from '@/utils/share'
...@@ -12,11 +13,16 @@ const router = createRouter({ ...@@ -12,11 +13,16 @@ const router = createRouter({
12 routes 13 routes
13 }); 14 });
14 15
15 -router.beforeEach((to, from, next) => { 16 +// TAG: 动态生成路由
16 - next() 17 +router.beforeEach((to) => { // next 不能用了,被return替代了
18 + if (!router.hasRoute(to.name)) { // 如果不存在
19 + router.addRoute(generateRoute(to)) // 新增路由
20 + // 触发重定向
21 + return to.fullPath
22 + }
17 }) 23 })
18 24
19 -router.afterEach((to, from, next) => { 25 +router.afterEach(() => {
20 // console.warn(to); 26 // console.warn(to);
21 // console.warn(wx); 27 // console.warn(wx);
22 // share(to) 28 // share(to)
......
1 +/**
2 + * 生成动态路由
3 + */
4 +import _ from 'lodash'
5 +import routes from "../mock/routes"
6 +
7 +// 根据后台返回的路径,生成页面的组件模版
8 +function loadView(component) {
9 + return () => import(`../views/${component}.vue`)
10 +}
11 +
12 +const formatRoutesArr = []
13 +
14 +routes.forEach(route => {
15 + const router = {}
16 + const {
17 + path,
18 + redirect,
19 + name,
20 + component,
21 + keepAlive,
22 + meta,
23 + children
24 + } = route
25 +
26 + router.component = loadView(component)
27 + if (redirect !== null) {
28 + router.redirect = redirect
29 + }
30 + if (keepAlive !== null) {
31 + router.keepAlive = keepAlive
32 + }
33 + if (children && children instanceof Array && children.length > 0) {
34 + router.children = formatRoutes(children)
35 + }
36 + if (name !== null) {
37 + router.name = name
38 + }
39 + if (meta !== null) {
40 + router.meta = meta
41 + }
42 + router.path = path
43 +
44 + formatRoutesArr.push(router)
45 +})
46 +
47 +export const generateRoute = (to) => {
48 + let router = ''
49 + _.each(formatRoutesArr, item => {
50 + if (item.path === to.path) {
51 + router = item
52 + }
53 + })
54 + return _.assign(to, router)
55 +}
...@@ -92,8 +92,6 @@ import { useRoute, useRouter } from 'vue-router' ...@@ -92,8 +92,6 @@ import { useRoute, useRouter } from 'vue-router'
92 import axios from '@/utils/axios'; 92 import axios from '@/utils/axios';
93 import { Toast } from 'vant'; 93 import { Toast } from 'vant';
94 94
95 -import IMAGE from '@/views/html2canvas.vue'
96 -
97 const $route = useRoute(); 95 const $route = useRoute();
98 const $router = useRouter(); 96 const $router = useRouter();
99 97
...@@ -131,28 +129,6 @@ axios.get('/srv/?a=kg_info') ...@@ -131,28 +129,6 @@ axios.get('/srv/?a=kg_info')
131 .catch(err => { 129 .catch(err => {
132 console.error(err); 130 console.error(err);
133 }); 131 });
134 -
135 -// 测试动态路由
136 -/**
137 - * , {
138 - path: '/image',
139 - name: 'html转图片',
140 - component: () => import('./views/html2canvas.vue'),
141 - meta: {
142 - title: ''
143 - }
144 - }
145 - */
146 -
147 -// $router.addRoute({ path: '/image', name: 'html转图片', meta: { title: 'html转图片' }, component: IMAGE})
148 -
149 -onMounted(() => {
150 - // setTimeout(() => {
151 - // $router.push({
152 - // path: '/image'
153 - // })
154 - // }, 3000);
155 -})
156 </script> 132 </script>
157 133
158 <script> 134 <script>
......