路由重定向
上一节
下一节
什么是路由重定向?
在开发过程中,可能希望当用户访问不同的路径时,页面中显示同一个组件,这时就需要用到路由重定向。路由重定向可以使用户在访问一个URL地址时,强制跳转到另一个URL地址,从而展示特定的组件。通过路由匹配规则中的redirect属性可以指定一个新的路由地址,从而实现路由重定向。
演示路由重定向的使用方法
修改src\router.js文件,实现当用户访问“/”路径时,将路由重定向到“/home”路径。
const router = createRouter({
history: createWebHashHistory(),
routes: [
{ path: '/', redirect: '/home' },
{ path: '/home', component: import ('./components/Home.vue') },
{ path: '/about', component: import('./components/About.vue') }
]
})

