- 安装依赖
npm install vue-router
- 在src目录下新建router文件夹,router文件夹新建index.ts。内容如下
// Your code here
import {
createRouter as _createRouter,
createMemoryHistory,
createWebHistory
} from 'vue-router'
import Layout from '@/layout'
/**
* Note: 路由配置项
*
* hidden: true // 当设置 true 的时候该路由不会再侧边栏出现 如401,login等页面,或者如一些编辑页面/edit/1
* alwaysShow: true // 当你一个路由下面的 children 声明的路由大于1个时,自动会变成嵌套的模式--如组件页面
* // 只有一个时,会将那个子路由当做根路由显示在侧边栏--如引导页面
* // 若你想不管路由下面的 children 声明的个数都显示你的根路由
* // 你可以设置 alwaysShow: true,这样它就会忽略之前定义的规则,一直显示根路由
* redirect: noRedirect // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
* name:'router-name' // 设定路由的名字,一定要填写不然使用<keep-alive>时会出现各种问题
* query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
* roles: ['admin', 'common'] // 访问路由的角色权限
* permissions: ['a:a:a', 'b:b:b'] // 访问路由的菜单权限
* meta : {
noCache: true // 如果设置为true,则不会被 <keep-alive> 缓存(默认 false)
title: 'title' // 设置该路由在侧边栏和面包屑中展示的名字
icon: 'svg-name' // 设置该路由的图标,对应路径src/assets/icons/svg
breadcrumb: false // 如果设置为false,则不会在breadcrumb面包屑中显示
activeMenu: '/system/user' // 当路由设置了该属性,则会高亮相对应的侧边栏。
}
*/
// 公共路由
export const constantRoutes = [
// {
// path: '/redirect',
// component: Layout,
// hidden: true,
// children: [
// {
// path: '/redirect/:path(.*)',
// component: () => import('@/views/redirect/index.vue')
// }
// ]
// },
// {
// path: '/login',
// component: () => import('@/views/login'),
// hidden: true
// },
// {
// path: '/register',
// component: () => import('@/views/register'),
// hidden: true
// },
// {
// path: "/:pathMatch(.*)*",
// component: () => import('@/views/error/404'),
// hidden: true
// },
// {
// path: '/401',
// component: () => import('@/views/error/401'),
// hidden: true
// },
{
path: '',
// component: Layout,
redirect: '/index',
children: [
{
path: '/index',
component: () => import('@/views/index.vue'),
name: 'Index',
meta: { title: '系统首页', icon: 'home', affix: true }
}
]
},
{
path: '/about',
name: 'about',
component: () => import('@/views/about.vue')
}
// {
// path: '/user',
// component: Layout,
// hidden: true,
// redirect: 'noredirect',
// children: [
// {
// path: 'profile',
// component: () => import('@/views/system/user/profile/index'),
// name: 'Profile',
// meta: { title: '个人中心', icon: 'user' }
// }
// ]
// }
]
// 动态路由,基于用户权限动态去加载
// export const dynamicRoutes = [
// {
// path: '/system/user-auth',
// component: Layout,
// hidden: true,
// permissions: ['system:user:edit'],
// children: [
// {
// path: 'role/:userId(\\d+)',
// component: () => import('@/views/system/user/authRole'),
// name: 'AuthRole',
// meta: { title: '分配角色', activeMenu: '/system/user' }
// }
// ]
// },
// {
// path: '/system/role-auth',
// component: Layout,
// hidden: true,
// permissions: ['system:role:edit'],
// children: [
// {
// path: 'user/:roleId(\\d+)',
// component: () => import('@/views/system/role/authUser'),
// name: 'AuthUser',
// meta: { title: '分配用户', activeMenu: '/system/role' }
// }
// ]
// },
// {
// path: '/system/dict-data',
// component: Layout,
// hidden: true,
// permissions: ['system:dict:list'],
// children: [
// {
// path: 'index/:dictId(\\d+)',
// component: () => import('@/views/system/dict/data'),
// name: 'Data',
// meta: { title: '字典数据', activeMenu: '/system/dict' }
// }
// ]
// }
// ]
export function createRouter() {
return _createRouter({
history: import.meta.env.SSR? createMemoryHistory('/'): createWebHistory('/'),
routes:constantRoutes,
//scrollBehavior,使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面
scrollBehavior(to, from, savedPosition) {
if (savedPosition) {
return savedPosition
} else {
return { top: 0 }
}
}
})
}
- app.vue使用路由改造
<router-view v-slot="{ Component }">
<transition name="fade">
<keep-alive>
<component :is="Component" />
</keep-alive>
</transition>
</router-view>
- transition 是一个过渡效果,当切换路由时,页面会有一个过渡动画的效果
- KeepAlive 是一个内置组件,它的功能是在多个组件间动态切换时缓存被移除的组件实例
- 默认情况下,一个组件实例在被替换掉后会被销毁。这会导致它丢失其中所有已变化的状态——当这个组件再一次被显示时,会创建一个只带有初始状态的新实例。
- keepAlive组件可以缓存被替换掉的组件实例,这样当它重新被显示时,就会恢复到之前的状态,而不会重新渲染。
- main.ts添加路由
import { createSSRApp } from 'vue'
import './style.css'
import App from './App.vue'
import { createRouter } from './router'
export const createApp = () => {
const app = createSSRApp(App)
const router = createRouter()
app.use(router)
return { app, router }
}
- entry-server.ts和entry-client.ts添加路由
entry-server.ts
import { renderToString } from 'vue/server-renderer'
import { createApp } from './main'
import { RouteLocationRaw } from 'vue-router'
export async function render(url:RouteLocationRaw) {
const { app,router } = createApp()
router.push(url)
await router.isReady()
const ctx = {}
const html = await renderToString(app, ctx)
return { html }
}
entry-client.ts
import './style.css'
import { createApp } from './main'
const { app, router } = createApp()
router.isReady().then(() => {
app.mount('#app')
})