Vue | 有关Vue2路由知识点的一些总结,以及Vue3路由做出了哪些调整?

news/2024/7/8 3:15:09

目录

Vue2:

1. 路由:

2. 路由规则:

 3. 实现切换(active-class可配置高亮样式)     

4. 指定展示位置     

5. 路由的query参数 

6. params传参: 

7. 多级路由

8. 路由的props配置  

9. 的replace属性 

10. 编程式路由导航 

11. 缓存路由组件

12. 两个新的生命周期钩子

Vue3: 


Vue2:

1. 路由

  1. 理解:一个路由(route)就是一组映射关系(key-value),多个路由需要路由器(router)进行管理。
  2. 前端路由:key是路径,value是组件

2. 路由规则:

编写router配置项(在router文件夹中的index.js文件中) 

       //引入VueRouter插件
       import VueRouter from 'vue-router'
       //引入路由组件
       import About from '../components/About
       import Home from '../componets/Home

       //创建router实例对象,去管理路由规则
       const router = new VueRouter({
         routes:[
           {
             path:'/about',
             component:About
           },
           {
             path:'/home',
             component:Home
           }
         ]
       })

       //暴露router
       export default router

 3. 实现切换(active-class可配置高亮样式)     

 <router-link active-class='active' to='/about'>About</router-link>

4. 指定展示位置     

<router-view></router-view>

5. 路由的query参数 

1)传递参数的组件(携带参数有【to的字符串】写法和【to的对象】写法):


      //跳转并携带query参数,to的字符串写法
      <router-link :to="`/home/message/detail?id=${m.id}&title=${m.title}`">{{m.title}}</router-link>

      //跳转并携带query参数,to的对象写法
      <router-link :to="{
          path:'/home/message/detail',
          query:{
            id:m.id,
            title:m.title
          }
        }">
          {{m.title}}
        </router-link>

 2)接收参数的组件:

<template>
  <ul>
      <li>消息编号:{{$route.query.id}}</li>
      <li>消息标题:{{$route.query.title}}</li>
  </ul>
</template>

6. params传参: 

首先我们要注意的第一个点:

 

1)传递参数的组件:

  <!--跳转路由并携带params参数,to的字符串写法-->
        <router-link :to="`/home/message/detail/${m.id}/${m.title}`">{{m.title}}</router-link>&nbsp;&nbsp;

        <!--跳转路由并携带params参数,to的对象写法-->
        <router-link
         :to="{
            name:'xiangqing',
            params:{
              id:m.id,
              title:m.title
           }
          }"
          >{{m.title}}
        </router-link>

特别注意:路由携带params参数时,若使用to的对象写法,则不能使用path配置项,必须使用name配置!! 

 2)接收参数的组件:

<template>
  <ul>
      <li>消息编号:{{$route.params.id}}</li>
      <li>消息标题:{{$route.params.title}}</li>
  </ul>
</template>

7. 多级路由

配置路由规则(在router文件中的index.js文件中)使用children配置项: 

        routes:[
        {
          path:'/about',
          component:About,
        },
        {
          path:'/home',
          component:Home,
          children:[//通过children配置子级路由
            {
              path:'news', //此处一定不要写成 /news
              component:News,
            },
            {
              path:'message',//同理可得
              component:Message,
            }
          ]
        }
      ]

 ps:children配置项里的path路径不要加【/】

8. 路由的props配置  

作用:让路由组件更方便的收到参数。

              {
                 path:'message',
                 component:Message,
                 children:[
                     {
                        name:'xiangqing',
                        path:'detail',
                        component:Detail, 
                        //props的第一种写法,值为对象,该对象中的所有key-value都会以props的形式传给Detail组件
                        //props:{a:1,b:'hello'}

                        //props的第二种写法,值为布尔值.若布尔值为真,就会把该路由组件收到的所有params参数,以props的形式传给Detail组件
                        //props:true

                        //props的第三种写法,值为函数(在query接收参数时)
                        props($route){
                            return {id:$route.query.id,title:$route.query.title}
                        }
                     }
                 ]
             }

 几个注意点:

  • 路由组件通常存放在pages文件夹中,一般组件通常存放在components文件
  • 通过切换,‘隐藏’了的路由组件,默认是被销毁掉的,需要的时候再去挂载
  • 每个组件都有自己的$route属性,里面存储自己的路由信息
  • 整个应用只有一个router,可以通过组件的$router属性获取到

9. <router-link>的replace属性 

  1. 作用:控制中由跳转时操作浏览器历史记录的模式
  2. 浏览器的历史有两种写入方式:分别为push和replace,push是追加历史记录,replace是替换当前记录,路由跳转时候默认为push
  3. 如何开启replace模式:<router-link replace……>News<router-link>

10. 编程式路由导航 

除了借助<router-link>实现路由跳转,我们还可以通过编程式路由跳转,它相比 <router-link>实现路由跳转来说,使用路由跳转更加的灵活。废话不多说,我们来看一下具体是怎么实现的:

结构:

<template>
   <div>
    <ul>
      <li v-for="m in messageList" :key="m.id">
          {{m.title}}
        <button @click="pushShow(m)">push查看</button>
        <button @click="replaceShow(m)">replace查看</button>
      </li>      
    </ul>
    <hr>
    <router-view></router-view>
   </div>
</template>

Click触发的事件:

 methods:{
      pushShow(m){
        this.$router.push({
          name:'xiangqing',
          query:{
            id:m.id,
            title:m.title
          }
        })
      },
      replaceShow(m){
        this.$router.replace({
          name:'xiangqing',
          query:{
            id:m.id,
            title:m.title
          }
        })
      }
    }
  }

 

  •       this.$router.forward()    前进
  •       this.$router.back()        后退
  •       this.$router.go(-2)         正数是前进几步,负数是后退几步

11. 缓存路由组件

作用:让不展示的路由组件保持挂载,不被销毁 

      <!--缓存多个组件-->
      <!--<keep-alive  :include="['News','Message']">-->
      <!--缓存一个组件-->
      <keep-alive  include="News">
        <router-view></router-view>
      </keep-alive>

 注意:News是组件名

12. 两个新的生命周期钩子

  1.  作用:路由组件所独有的两个钩子,用于捕获路由组件的激活状态
  2. 具体名字:

       1)activated 路由组件被激活时触发

       2)deactivated 路由组件失活时触发。 

    activated(){
      //激活
      console.log('激活了')
      this.timer = setInterval(() =>{
        this.opacity -= 0.01
        if(this.opacity <= 0)this.opacity = 1
      },16)
    },
    deactivated(){
      //失活
       console.log('失活了')
       clearInterval(this.timer)
    }

Vue3: 

Vue2的路由基本介绍完了,那么在Vue3中,又作出了哪些调整呢?

在index.js中:

 在main.js中:

 VUE3中某些组件使用:

  • import { useRouter, useRoute } from 'vue-router' 【按需引入】
  • useRouter, useRoute是方法,分别相当于VUE2中的$router、$route
<script setup>
	import { ref, watch } from 'vue'
	import { useRouter, useRoute } from 'vue-router'
	const router = useRouter() // 和 vue2中的this.$router 是一样的
	const toDetail = (id) => {
		router.push({ 
			path: `/detail`
			query: {
				id: id
			}
		})
	}
	
	const route = useRoute() // 和 vue2中的this.$route 是一样的
	watch(route, () => {
	    // ...
	})
</script>


http://www.niftyadmin.cn/n/15337.html

相关文章

UE在TextRender上显示中文文本

文章目录 1.实现目标2.实现过程2.1 添加字体2.2 创建字体材质2.3 将字体应用到TextRender3.参考资料1.实现目标 UE的UMG可以正常支持中文,但是TextRender并不支持中文,因此需要添加本地离线中文字体库,使TextRender显示中文。 2.实现过程 2.1 添加字体 (1)选择User Int…

C++经典题目

目录 P62 3.6 求圆周长面积 P80 3 华氏转摄氏 P80 10 分段函数 P81 21 数列求和 P82 24 打印图形 P229 6 长方体体积 P384 4 printArea 题目来源于C程序设计&#xff08;第4版&#xff09; P62 3.6 求圆周长面积 设圆半径r1.5&#xff0c;圆柱高h3&#xff0c;求圆周长…

Java_笔记_多态_包_final_权限修饰符_代码块

封装&#xff1a;对象代表什么&#xff0c;就得封装对应的数据&#xff0c;并提供数据对应的行为。 一、多态&#xff1a;对象的多种形态。同类型的对象&#xff0c;表现出的不同形态。 1.多态的表现形式&#xff1a;父类类型 对象名称 子类对象; 学生形态 对象 Student s …

Unity Cg着色器开发教程

Unity Cg着色器开发教程 学习在 Unity 中对图形管道进行编程&#xff0c;以便为游戏对象创建独特的视觉表面 课程英文名&#xff1a;Shader Development from Scratch for Unity with Cg 此视频教程共2.0小时&#xff0c;中英双语字幕&#xff0c;画质清晰无水印&#xff0c…

StarRocks技术内幕 | 打造一款强大成熟的数据库有多难?

作者&#xff1a;康凯森&#xff0c;StarRocks PMC&#xff0c;负责查询方向的研发&#xff08;本文转自其个人博客“编程小梦”&#xff09; 从 2015 年开始&#xff0c;我在美团先后维护和研发过 Apache HBase、Apache Kylin、Apache Druid 和 Apache Doris&#xff0c;对大…

全面上新!阿里 2023 版(Java 岗)面试突击手册,Github 已标星 37K

程序员面试背八股&#xff0c;几乎已经是互联网不可逆的一个形式了。自从面试**八股文火了之后&#xff0c;网上出现了不少 Java 相关的面试题&#xff0c;很多朋友盲目收集背诵&#xff0c;**但网上大部分的面试题&#xff0c;大多存在这几个问题&#xff1a;第一&#xff0c;…

Python对json的操作总结

Json简介&#xff1a;Json&#xff0c;全名 JavaScript Object Notation&#xff0c;是一种轻量级的数据交换格式。Json最广泛的应用是作为AJAX中web服务器和客户端的通讯的数据格式。现在也常用于http请求中&#xff0c;所以对json的各种学习&#xff0c;是自然而然的事情。 J…

pytorch基础操作(三)梯度下降(小批量)计算线性回归

1、线性模型 线性假设是指⽬标&#xff08;房屋价格&#xff09;可以表⽰为特征&#xff08;⾯积和房龄&#xff09;的加权和&#xff0c;如下⾯的式⼦&#xff1a; price warea area wage age b. 其中: warea和wage 称为权重&#xff08;weight&#xff09;&#xff0c;…