alias
2023年05月19日
一、认识
别名是指 将 /
别名为 /home
,意味着当用户访问 /home
时,URL
仍然是 /home
,但会被匹配为用户正在访问 /
。
二、语法
2.1 单个别名
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
const routes = [
{
path: '/',
name: 'home',
alias: '/home',
component: HomeView
},
{
path: '/about',
name: 'about',
component: () => import('../views/AboutView.vue')
},
]
const router = createRouter({
routes,
history: createWebHistory(import.meta.env.BASE_URL)
})
export default router
测试
<template>
<div>
<router-link to="/">首页</router-link>
<router-link to="/about">关于</router-link>
<router-link to="/home">Home</router-link>
<router-view></router-view>
</div>
</template>
2.2 多个别名
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
const routes = [
{
path: '/',
name: 'home',
alias: ['/home','/home1'],
component: HomeView
},
{
path: '/about',
name: 'about',
component: () => import('../views/AboutView.vue')
},
]
const router = createRouter({
routes,
history: createWebHistory(import.meta.env.BASE_URL)
})
export default router
测试
<template>
<div>
<router-link to="/">首页</router-link>
<router-link to="/about">关于</router-link>
<router-link to="/home">Home</router-link>
<router-link to="/home1">Home 1</router-link>
<router-view></router-view>
</div>
</template>