npm i vue-router -S
//引入 import router from './router' //使用 - 链式 app.use(router).mount('#app')
属性 | 说明 |
---|---|
history | createWebHistory:大多 WEB 应用使用的模式,SEO 优化好;需要正确配置服务器
createWebHashHistory:基于 hash;SEO 优化体现一般;无须额外配置服务器 |
routes | 路由列表;详细配置属性使用见下表 |
linkActiveClass?
linkExactActiveClass? |
路由匹配样式
严格路由匹配样式 |
http://localhost:5173/
import { createRouter, createWebHistory } from 'vue-router' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), // })
http://localhost:5173/#/
import { createRouter, createWebHashHistory } from 'vue-router' const router = createRouter({ history: createWebHashHistory(import.meta.env.BASE_URL), // })
属性 | 说明 |
---|---|
path | 路由;必填 |
component | 对应路由的组件;必填 |
name | 命名路由:通过名字来指定路由;不能重复;更直观、灵活,简化路由使用,如动态路由 |
redirect | 重定向路由:让指定的路由重新定位到另外一个路由 |
children | 嵌套路由、子路由;嵌套路由通常需要使用重定向 |
props | 是否启用路由参数;可以接收动态路由参数和查询路由参数 |
meta | 路由元信息:用于路由组件的额外配置;无法在编程式路由中定义或覆盖 |
beforeEnter | 路由独享的守卫 |
{ path: '/menu', component: () => import('@/views/MenuView.vue'), }
https://localhost:5173/menu?user=glpla.github.io
{ path: '/details/:id', component: () => import('@/views/DetailsView.vue'), }
https://localhost:5173/details?id=1001
{ path: '/:pathMatch(.*)*', name: 'NotFound', component: () => import('../views/NotFound.vue') }
import {h} from 'vue'
{ path: '/goods:pathMatch(.*)*', name: 'goods-not-found', component: h('p', {style: 'color: red'}, 'goods 404 Not Found') }
import TeamView from "@/views/AboutView.vue";
{ path: '/about', component: TeamView, }
{ path: '/about', component: () => import("@/views/AboutView.vue") }
{ path: '/center/login', component: () => import("@/views/LoginView.vue") name: 'login' }
{ path: '/', // redirect: '/home', redirect: { name: 'home' }, }, { path: '/home', name: 'home' component: () => import("@/views/HomeView.vue") }
{ path: "/menu", component: () => import("@/views/MenuView.vue"), name: "menu", children: [ { path: "goods", // 省略父级路由 component: () => import("@/components/Goods.vue"), name: "goods" }, { path: "vip", // 省略父级路由 component: () => import("@/components/Vip.vue"), name: "vip" }, { path: "/menu/rank", // 可以使用完整路由;建议所有子路由配置保持一致 component: () => import("@/components/Rank.vue"), name: "rank" }, { path: "/menu/favorite", component: () => import("@/components/Favorite.vue"), name: "favorite" } ] }
{ path: "/menu", component: () => import("@/views/MenuView.vue"), name: "menu", children: [ { path: "", //空路由 component: () => import("@/components/Goods.vue"), name: "goods" }, // ... ] }
{ path: '/menu', redirect: { name: "goods" }, // 使用命名路由 // redirect: '/menu/goods', // 或使用完整路由 component: () => import('@/views/MenuView.vue'), name: "menu", children:[ { path: 'goods', component: () => import('@/components/Goods.vue'), name: "goods" }, // ... ] }
{ path: "/mall", name: "mall", component: () => import("@/views/MallView.vue"), meta: { showNav: true, title: "瑞幸电商" }, },
. path
. component
. name
. redirect
. children
. meta
export const routes = [ { path: "/", name: "home", component: () => import("@/views/HomeView.vue"), }, // ... ];
import { routes } from "../assets/routes";
export default [ { path: "/", name: "home", component: () => import("@/views/HomeView.vue"), }, // ... ];
import routes from "../assets/routes";