VUE3 Router学习 第一章 路由框架以及介绍、命名路由-编程式导航、历史记录(replace、go、back)、路由传参、嵌套路由、命名视图、重定向-别名

一. 前言(路由框架以及介绍)

1. router 路由 介绍

应为vue是单页应用不会有那么多html 让我们跳转 所有要使用路由做页面的跳转
Vue 路由允许我们通过不同的 URL 访问不同的内容。通过 Vue 可以实现多视图的单页Web应用

2. 安装

构建前端项目

npm init vue@latest
//或者
npm init vite@latest

npm install vue-router@4

使用Vue3 安装对应的router4版本

使用Vue2安装对应的router3版本

3. 架构

在src目录下面新建router 文件 然后在router 文件夹下面新建 index.ts

//引入路由对象
import { createRouter, createWebHistory, createWebHashHistory, createMemoryHistory, RouteRecordRaw } from 'vue-router'
 
//vue2 mode history vue3 createWebHistory
//vue2 mode  hash  vue3  createWebHashHistory    一般用 hash
//vue2 mode abstact vue3  createMemoryHistory    对应 v2的三种路由模式  这个 ssr服务端渲染使用的
 
//路由数组的类型 RouteRecordRaw
// 定义一些路由
// 每个路由都需要映射到一个组件。
const routes: Array<RouteRecordRaw> = [{
    path: '/',
    component: () => import('../components/a.vue')
},{
    path: '/register',
    component: () => import('../components/b.vue')
}]
 
 
 
const router = createRouter({
    history: createWebHistory(),
    routes
})
 
//导出router
export default router

4. 使用

1. router-link#

请注意,我们没有使用常规的 a 标签,而是使用一个自定义组件 router-link 来创建链接。这使得 Vue Router 可以在不重新加载页面的情况下更改 URL,处理 URL 的生成以及编码。我们将在后面看到如何从这些功能中获益。

2. router-view#

router-view 将显示与 url 对应的组件。你可以把它放在任何地方,以适应你的布局。

<template>
  <div>
    <h1>小满最骚</h1>
    <div>
    <!--使用 router-link 组件进行导航 -->
    <!--通过传递 `to` 来指定链接 -->
    <!--`<router-link>` 将呈现一个带有正确 `href` 属性的 `<a>` 标签-->
      <router-link tag="div" to="/">跳转a</router-link>
      <router-link tag="div" style="margin-left:200px" to="/register">跳转b</router-link>
    </div>
    <hr />
    <!-- 路由出口 -->
    <!-- 路由匹配到的组件将渲染在这里 -->
    <router-view></router-view>
  </div>
</template>

5. 最后在main.ts 挂载

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
createApp(App).use(router).mount('#app')

二. 命名路由-编程式导航

1. 命名路由

除了 path 之外,你还可以为任何路由提供 name。这有以下优点:

  1. 没有硬编码的 URL
  2. params 的自动编码/解码。
  3. 防止你在 url 中出现打字错误。
  4. 绕过路径排序(如显示一个)
const routes:Array<RouteRecordRaw> = [
    {
        path:"/",
        name:"Login",  // 就是添加了一个 name 属性
        component:()=> import('../components/login.vue')
    },
    {
        path:"/reg",
        name:"Reg", // 就是添加了一个 name 属性
        component:()=> import('../components/reg.vue')
    }
]

router-link跳转方式需要改变 变为对象并且有对应name

<div>
      <router-link :to="{name:'Login'}">Login</router-link>  // 这里 to 加 : 以name 属性来跳转
      <router-link style="margin-left:10px" :to="{name:'Reg'}">Reg</router-link>
</div>

2. 编程式导航

除了使用 < router-link> 创建 a 标签来定义导航链接,**我们还可以借助 router 的实例方法,通过编写代码来实现
**

  1. 字符串模式
import { useRouter } from 'vue-router'
const router = useRouter()
 
const toPage = () => {
  router.push('/reg')  
}
  1. 对象模式
import { useRouter } from 'vue-router'
const router = useRouter()
 
const toPage = () => {
  router.push({
    path: '/reg'
  })
}
  1. 命名式路由模式
import { useRouter } from 'vue-router'
const router = useRouter()
 
const toPage = () => {
  router.push({
    name: 'Reg'
  })
}

3. a标签跳转

直接通过a href也可以跳转但是会刷新页面

 <a href="/reg">rrr</a>

三. 历史记录(replace、go、back)

1. replace的使用

采用replace进行页面的跳转会同样也会创建渲染新的Vue组件,但是在history中其不会重复保存记录,而是替换原有的vue组件;

1. router-link 使用方法 (直接在标签那 添加 replace 属性)

   <router-link replace to="/">Login</router-link>
   <router-link replace style="margin-left:10px" to="/reg">Reg</router-link>

2. 编程式导航

  <button @click="toPage('/')">Login</button>
  <button @click="toPage('/reg')">Reg</button>


import { useRouter } from 'vue-router'
const router = useRouter()
 
const toPage = (url: string) => {
  router.replace(url)   // 这路 不用 push  用 replace
}

2. 横跨历史(go 和 back)

该方法采用一个整数作为参数,表示在历史堆栈中前进或后退多少步( 注意 replace 模式下 无法 用 go 和 back

 <button @click="next">前进</button>
 <button @click="prev">后退</button>

const next = () => {
  //前进 数量不限于1
  router.go(1)
}
 
const prev = () => {
  //后退
  router.back()  // 当然  go(-1)  也是后退 
}

四. 路由传参

0. 三者的区别

  1. query 传参配置的是 path,而 params 传参配置的是name,在 params中配置 path 无效
  2. query 在路由配置不需要设置参数,而 params 必须设置
  3. query 传递的参数会显示在地址栏中(params 不会展现在 url 上 而是在内存里面)
  4. params传参刷新会无效,但是 query 会保存传递过来的值,刷新不变 ;(所以用动态路由的形式,就可以保存 params 传的值)
  5. 路由配置

1. Query路由传参

编程式导航 使用router push 或者 replace 的时候 改为对象形式新增query 必须传入一个对象


const toDetail = (item: Item) => {
    router.push({
        path: '/reg',
        query: item  //item 必须是一个对象
    })
}

接受参数
使用 useRoute 的 query // 注意 这个 useRoute 没有 r

import { useRoute } from 'vue-router';
const route = useRoute()


 <div>品牌:{{ route.query?.name }}</div> // 这里打 ? 怕他没有 报错
 <div>价格:{{ route.query?.price }}</div>
 <div>ID{{ route.query?.id }}</div>

2. Params路由传参

编程式导航 使用router push 或者 replace 的时候 改为对象形式并且只能使用name,path无效,然后传入params


const toDetail = (item: Item) => {
    router.push({
        name: 'Reg',
        params: item
    })
}

接受参数
使用 useRoute 的 params


import { useRoute } from 'vue-router';
const route = useRoute()

<div>品牌:{{ route.params?.name }}</div>
<div>价格:{{ route.params?.price }}</div>
<div>ID{{ route.params?.id }}</div>

3. 动态路由传参

很多时候,我们需要将给定匹配模式的路由映射到同一个组件。例如,我们可能有一个 User 组件,它应该对所有用户进行渲染,但用户 ID 不同。在 Vue Router 中,我们可以在路径中使用一个动态字段来实现,我们称之为 路径参数

路径参数 用冒号 : 表示。当一个路由被匹配时,它的 params 的值将在每个组件


const routes:Array<RouteRecordRaw> = [
    {
        path:"/",
        name:"Login",
        component:()=> import('../components/login.vue')
    },
    {
        //动态路由参数
        path:"/reg/:id",  // 这里
        name:"Reg",
        component:()=> import('../components/reg.vue')
    }
]
const toDetail = (item: Item) => {
    router.push({
        name: 'Reg',
        params: {
            id: item.id  // 注意 params 里面必须是对象的形式
        }
    })
}
import { useRoute } from 'vue-router';
import { data } from './list.json'
const route = useRoute()
 
 
const item = data.find(v => v.id === Number(route.params.id))
// find  如果是 true 则返回该数据

五. 嵌套路由

一些应用程序的 UI 由多层嵌套的组件组成。在这种情况下,URL 的片段通常对应于特定的嵌套组件结构,例如:

const routes: Array<RouteRecordRaw> = [
    {
        path: "/user",
        component: () => import('../components/footer.vue'),
        children: [   // 可以不停的嵌套 加 children
            {
                path: "",
                name: "Login",
                component: () => import('../components/login.vue')
            },
            {
                path: "reg",
                name: "Reg",
                component: () => import('../components/reg.vue')
            }
        ]
    },
 
]

如你所见,children 配置只是另一个路由数组,就像 routes 本身一样。因此,你可以根据自己的需要,不断地嵌套视图

<div>
        <router-view></router-view>
        <div>
            <router-link to="/">login</router-link>
            <router-link style="margin-left:10px;" to="/user/reg">reg</router-link> //  这里 子路由的跳转 记得加父路由的 usl
        </div>
    </div>

六. 命名视图

命名视图可以在同一级(同一个组件)中展示更多的路由视图,而不是嵌套显示。 命名视图可以让一个组件中具有多个路由渲染出口,这对于一些特定的布局组件非常有用。 命名视图的概念非常类似于“具名插槽”,并且视图的默认名称也是 default。

一个视图使用一个组件渲染,因此对于同个路由,多个视图就需要多个组件**。确保正确使用 components 配置 (带上 s)**

import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
 
const routes: Array<RouteRecordRaw> = [
    {
        path: "/",
        components: {
            default: () => import('../components/layout/menu.vue'), // default 表示 默认的这个
            header: () => import('../components/layout/header.vue'),
            content: () => import('../components/layout/content.vue'),
        }
    },
]
 
const router = createRouter({
    history: createWebHistory(),
    routes
})
 
export default router

    <div>
        <router-view></router-view>
        <router-view name="header"></router-view>
        <router-view name="content"></router-view>
    </div>

七. 重定向-别名

1. 重定向 redirect

1. 字符串形式配置,访问/ 重定向到 /user (地址栏显示/,内容为/user路由的内容)

const routes: Array<RouteRecordRaw> = [
    {
        path:'/',
        component:()=> import('../components/root.vue'),
        redirect:'/user1', // 这里
        children:[
            {
                path:'/user1',
                components:{
                    default:()=> import('../components/A.vue')
                }
            },
            {
                path:'/user2',
                components:{
                    bbb:()=> import('../components/B.vue'),
                    ccc:()=> import('../components/C.vue')
                }
            }
        ]
    }
]

** 2. 对象形式配置**

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        component: () => import('../components/root.vue'),
        redirect: { path: '/user1' }, // 这里
        children: [
            {
                path: '/user1',
                components: {
                    default: () => import('../components/A.vue')
                }
            },
            {
                path: '/user2',
                components: {
                    bbb: () => import('../components/B.vue'),
                    ccc: () => import('../components/C.vue')
                }
            }
        ]
    }
]

3. 函数模式(可以传参)

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        component: () => import('../components/root.vue'),
        redirect: (to) => {  // 这里
            return {
                path: '/user1',
                query: to.query // 这里
            }
        },
        children: [
            {
                path: '/user1',
                components: {
                    default: () => import('../components/A.vue')
                }
            },
            {
                path: '/user2',
                components: {
                    bbb: () => import('../components/B.vue'),
                    ccc: () => import('../components/C.vue')
                }
            }
        ]
    }
]

2. 别名 alias (跟redirect 刚好相反)

将 / 别名为 /root,意味着当用户访问 /root时,URL 仍然是 /user,但会被匹配为用户正在访问 /

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        component: () => import('../components/root.vue'),
        alias:["/root","/root2","/root3"], // url选择 /root3 页面 还是 /
        children: [
            {
                path: 'user1',
                components: {
                    default: () => import('../components/A.vue')
                }
            },
            {
                path: 'user2',
                components: {
                    bbb: () => import('../components/B.vue'),
                    ccc: () => import('../components/C.vue')
                }
            }
        ]
    }
]

版权声明:本文为m0_55170432原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。