Vue2路由
1、vue的路由默认是hash模式,修改为history,两者的区别:https://blog.csdn.net/yexudengzhidao/article/details/87689960
2、路由懒加载,把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了:https://router.vuejs.org/zh/guide/advanced/lazy-loading.html
代码:
const home = () => import('@/views/home/index');
把component那里改成component:home就好了。
动态路由:https://www.jb51.net/article/190172.htm
在src/router/index.js中
import Vue from 'vue' import VueRouter from 'vue-router' import store from '@/store' import { setToken, getToken, removeToken } from "@/utils/cookies"; import { getAllPermissions, getRoles, getCurrentLoginInformations, getAllRoutes } from '@/api/user' import NProgress from 'nprogress' Vue.use(VueRouter) import { asyncRoutes } from "@/utils/constData"; export const _routes = [ { path: '/', name: 'default', redirect: '/login', meta: { title: '登录' } }, { path: '/login', name: 'login', component: () => import('@/views/login/index'), meta: { title: '登录' } }, { path: '/welcome', name: 'welcome', component: () => import('@/views/welcome/index'), meta: { title: '欢迎使用' } }, { path: '/404', name: '404', meta: { title: '404' }, component: () => import('../components/404.vue'), }, { path: '/', redirect: '/login' }, ]; const router = new VueRouter({ routes: _routes, mode: 'history', // 解决vue框架页面跳转有白色不可追踪色块的bug scrollBehavior: () => ({ x: 0, y: 0 }) }); let hasGen = false; router.beforeEach((to, from, next) => { NProgress.start(); let token = getToken(); if (to.path === "/login") { if (token != '' && token != undefined) { next({ path: "/welcome" }); NProgress.done(); } else { next(); NProgress.done(); } } else if (to.path === "/404") { next(); NProgress.done(); } else { if (token != '' && token != undefined) { if (!hasGen) { hasGen = true; getAllRoutes().then((res) => { let ritems = res.data; let nroutes = []; for (let i = 0; i < ritems.length; i++) { if (asyncRoutes[ritems[i].key]) { nroutes.push(asyncRoutes[ritems[i].key]); } } router.addRoutes(nroutes); }); } next(); NProgress.done(); } else { next({ path: "/login" }); NProgress.done(); } } }); //前置钩子,导航守卫,跳转下一步之前执行 // router.beforeEach((to, from, next) => { // document.title = to.matched[0].meta.title; // next(); // }); // router.afterEach((to, from) => { // }); export default router
constData.js
// 本地所有的页面 需要配合后台返回的数据生成页面 export const asyncRoutes = { system: { path: '/system', name: '系统管理', meta: { title: '系统管理' }, component: () => import('@/views/system/index'), }, article: { path: '/article', name: '文章管理', meta: { title: '文章管理' }, component: () => import('@/views/article/index'), }, };
注:把基本的不需要动态生成路由写到router里;把需要动态生成的单独写到一个js文件中;后台根据登录用户的权限返回需要生成的路由的key;根据返回的key过滤出来需要的路由;使用router.addRoutes()把集合添加到路由中。
路由传值:
传参:<router-link :to="{path:'/profiledetail',query:{name:'jay.x'}}" tag='button'>详情</router-link>,this.$router.push({path: '/profiledetail', query:{name:'jay.x'}}); 获取:this.$route.query.name;
传参:<router-link :to="'/profiledetail?name=jay.x'" tag='button'>详情</router-link>,this.$router.push({name: 'profiledetail', params:{name:'jay.x'}});获取:this.$route.params.name;
Vue3路由,用的是 npm install vue-router@4 官网:https://router.vuejs.org/zh/guide/
import { createRouter, createWebHistory } from 'vue-router' import home from '@/views/home/index' const _routes = [ { path: '/', redirect: "/home", }, { path: '/home', component: home, }, { path: '/login', component: () => import('@/views/home/login') } ]; const routerObj = createRouter({ history: createWebHistory(), routes: _routes }); export default routerObj
import { createApp } from 'vue'
import App from './App.vue'
import routerObj from '@/router/index'
const app = createApp(App);
app.use(routerObj);
app.mount('#app');
Vue3路由传参与获取有改变:https://router.vuejs.org/zh/guide/essentials/passing-props.html
在setup中访问路由和当前路由:https://router.vuejs.org/zh/guide/advanced/composition-api.html
传参:router.push({path: '/art2', query:{address:'jay.x'}});
接收:
<script setup> import { ref, reactive, computed, onMounted } from 'vue'; import { useRouter, useRoute,onBeforeRouteLeave, onBeforeRouteUpdate } from 'vue-router' const router = useRouter(); const route = useRoute(); // console.log(route); let addr = route.query.address; onMounted(() => { }); </script>