Pinia+Router学习笔记(十二)

简介: 本节记录Vue-Router的两种路由重定向方式

路由重定向有两种类型,一种是redirect,还有一种是alisa:

  1. redirect:用来将当前URL重定向到新的URL
  2. alisa:为路由起别名,不同路径跳转到的实际上是同一个组件
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Login',
        redirect: '/user',
    },
    {
        path: '/user',
        name: 'Reg',
        component: import('../components/reg.vue'),
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

export default router

此时访问/相当俞访问/user

redire的两种写法

第一种:直接使用字符串,示例在上面
第二种:对象写法(path会自动对应到其他路由中)

import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Login',
    redirect: {path:'/user'},
  },
  {
    path: '/user',
    name: 'Reg',
    component: import('../components/reg.vue'),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

export default router

第三种:函数写法(支持传入一个参数to,内部包含跳转之前页面的信息)

import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Login',
        redirect: (to) => {
            console.log(to)
            return {
                path: '/user',
                query: {
                    xiaoman: '小满',
                },
            }
        },
    },
    {
        path: '/user',
        name: 'Reg',
        component: () => import('../components/reg.vue'),
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

export default router

alisa属性

前排提醒:若要使用alisa则属性中必须有redirect,否则ts将报类型错误

import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Login',
        alias: ['/root1', '/root2'],
        redirect: (to) => {
            console.log(to)
            return {
                path: '/user',
                query: {
                    xiaoman: '小满',
                },
            }
        },
    },
    {
        path: '/user',
        name: 'Reg',
        component: () => import('../components/reg.vue'),
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

export default router

此时,访问/#/root1和/#/root2跳转到的页面和直接访问/相同

相关文章
|
23天前
|
前端开发
如何使用react-router v6快速搭建路由?
如何使用react-router v6快速搭建路由?
68 0
|
22天前
|
JavaScript 网络架构 开发者
阿珊解说Vue中`$route`和`$router`的区别
阿珊解说Vue中`$route`和`$router`的区别
|
8月前
|
移动开发 前端开发 API
React-Router-基本使用
React-Router-基本使用
39 0
|
10月前
|
前端开发
前端学习笔记202304学习笔记第十九天-vue3.0-vue2的项目中创建路由1
前端学习笔记202304学习笔记第十九天-vue3.0-vue2的项目中创建路由1
50 0
|
12月前
|
存储 JavaScript 前端开发
Vue —— 进阶 vue-router 路由(零)(路由的概念、基本使用)
Vue —— 进阶 vue-router 路由(零)(路由的概念、基本使用)
Pinia+Router学习笔记(十一)
本节记录嵌套路由&命名视图相关内容
87 0
Pinia+Router学习笔记(十四)
本节记录Vue-Router中路由过渡动效相关内容
75 0
|
前端开发
Pinia+Router学习笔记(十五)
本节记录Vue-Router中关于路由滚动行为的相关内容
55 0
|
前端开发 网络架构
Pinia+Router学习笔记(十六)
本节记录Vue-Router中动态路由相关内容
152 0
|
前端开发
react实战笔记160:Route组件1
react实战笔记160:Route组件1
44 0
react实战笔记160:Route组件1

热门文章

最新文章