首页 > 其他分享 >Vue路由使用总结

Vue路由使用总结

时间:2023-06-12 14:11:56浏览次数:64  
标签:总结 Vue component 组件 router msg path 路由

1、多级路由

(1)配置路由规则,使用children配置项:

// 编写配置项
const router = new VueRouter({
  routes: [
    {
      path: '/about',
      component:About,
    },
    {
      path: '/home',
      component: Home,
      children: [
        {
          path: 'news', // 此处一定不要写:/news
          component: News,
        },
        {
          path: 'message',// 此处一定不要写:/message
          component: Message,
        }
      ]
    }
  ]
})


export default router

(2)跳转(要写完整路径)

 <router-link to="/home/news">News</router-link>

2、路由的query参数

1、传递参数

 <ul class="list">
      <li
        v-for="item in list"
        :key="item.id"
      >
        <!-- 跳转并携带query参数,to的字符串写法 -->
        <router-link :to="`/home/message/detail?id=${item.id}&msg=${item.msg}`">{{item.msg}}</router-link>

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

2、接受参数

$route.query.id
$route.query.msg

3、命名路由

1、作用:可以简化路由的跳转

2、如何使用

  (1)给路由命名 

// 编写配置项
const router = new VueRouter({
  routes: [
    {
      path: '/home',
      component: Home,
      children: [
        {
          path: 'message',// 此处一定不要写:/message
          name:'message', // 给路由命名
          component: Message,
          children: [
            {
              name: 'detail',
              path: 'detail',
              component:Detail
            }
          ]
        }
      ]
    }
  ]
})

  (2)简化跳转

<!-- 简化前,要写完整的路径 -->
<router-link to="/home/message/detail">Message</router-link>

<!-- 简化后,直接通过路由名字跳转 -->
<router-link :to="{name:detail}">Message</router-link>

<!-- 简化写法配合传递参数 -->
 <router-link :to="{
          name:'detail',
          query:{
            id:item.id,
            msg:item.msg
          }
}">{{item.msg}}</router-link>

          

4、路由的params参数

1、配置路由,声明接受的params参数

{
      path: '/home',
      component: Home,
      children: [
        {
          path: 'news', // 此处一定不要写:/news
          component: News,
        },
        {
          path: 'message',
          name:'message',
          component: Message,
          children: [
            {
              name: 'detail',
              path: 'detail/:id/:msg', //使用占位符声明接收params参数
              component:Detail
            }
          ]
        }
      ]
    }

2、传递参数

<li
        v-for="item in list"
        :key="item.id"
      >
        <!-- 跳转并携带params参数,to的字符串写法 -->
        <router-link :to="`/home/message/detail/${item.id}/${item.msg}`">{{item.msg}}</router-link>

        <!-- 跳转并携带params参数,to的对象写法 -->
        <router-link :to="{
          name:'detail',
          params:{
            id:item.id,
            msg:item.msg
          }
        }">{{item.msg}}</router-link>
      </li>

3、接受参数

$route.params.id
$route.params.msg 

5、路由的props配置

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

{
  name: 'detail',
   path: 'detail/:id/:msg', //使用占位符声明接收params参数
   component: Detail,
   // 第一种写法,props值为对象,该对象中所有的key-value的组合最终都会通过props传给Detail组件
   // props: { a: 900 },

   // 第二种写法:prop值为布尔值,布尔值为true,则把路由收到的所有的params参数通过props传给Detail组件
   // props:true,

   // 第三种写法:props值为函数,该函数返回对象中每一组key-value都会通过props传递给Detail组件
    props(route) { 
      return {
         id: route.query.id,
         msg:route.query.msg
        }
     }

2、组件类接受

 props:['id','msg'], // 接受路由参数

6、<router-link>的replace属性

1、作用:控制路由跳转时操作浏览器历史记录的模式

2、浏览器的历史记录有两种写入方式:分别为铺设和replace,push是追加历史记录,replace是替换当前记录,路由跳转时候默认为push

3、如何开启replace模式<router-link replace .... >News<router-link>

7、编程式路由导航

1、作用:不借助<router-link>实现路由跳转,让路由跳转更加灵活

2、具体编码:

  // $router的两个API
    this.$router.push({
      name: 'detail',
      params: {
        id: 'xxx',
        msg:'xxxx'
      }
    })


    this.$router.replace({
      name: 'detail',
      params: {
        id: 'xxx',
        msg:'xxxx'
      }
    })

    this.$router.forward() //前进
    this.$router.back() // 后退
    this.$router.go() //传入数字,数字为正数表示前进,数字为负数表示后退,0表示刷新当前页面

8、缓存路由组件

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

2、具体编码:

<keep-alive include="News">
      <router-view></router-view>
</keep-alive>

9、与路由相关的两个生命周期钩子

1、作用:路由组件所独有的两个钩子,用于捕获路由组件的激活状态

2、具体名字:

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

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

10、路由守卫

1、作用:路由组件所独有的两个钩子,用于捕获路由组件的激活状态

2、分类:全局守卫,独享守卫,组件内守卫

3、全局守卫

// 全局前置守卫,初始化时执行,每次路由切换前执行
router.beforeEach((to,from,next) => { 
  console.log('beforeEach',to, from);
  if (to.meta.isAuth) { //meta 是路由配置,可以配置一些要用的额外信息,如权限信息
    if (localStorage.getItem('user') === 'test') {
      next() // 放行
    } else {
      console.log('暂无权限查看')
    }
  } else { 
    next() // 放行
  }
})


// 全局后置守卫,初始化时执行,每次路由切换前执行
router.afterEach((to,from) => { 
  console.log('afterEach', to, from);
  if (to.meta.title) { 
    document.title = to.meta.title // 修改网页的title
  } else {
    document.title = 'vue_test'
  }
})

export default router

4、独享守卫

 {
     path: 'news', // 此处一定不要写:/news
     component: News,
     meta: {isAuth:true,title:'新闻' },
     beforeEnterr(to,from,next) { 
     console.log('beforeEnterr',to, from);
     if (to.meta.isAuth) { 
          if (localStorage.getItem('user') === 'test') {
            next() // 放行
          } else {
            console.log('暂无权限查看')
          }
     } else { 
         next() // 放行
      }
    },
},

5、组件内守卫

 // 进入守卫,通过路由规则,进入该组件时被调用
  beforeRouteEnter(to, from, next) { 
    
  },
  // 离开守卫,通过路由规则,离开该组件时被调用
  beforeRouteLeave(to,from,next) { 

  },

标签:总结,Vue,component,组件,router,msg,path,路由
From: https://www.cnblogs.com/hyt09/p/17474387.html

相关文章

  • 直播系统搭建,vue插件之vue-seamless-scroll 无缝滚动
    直播系统搭建,vue插件之vue-seamless-scroll无缝滚动使用 importvueSeamlessScrollfrom'vue-seamless-scroll' //注册组件 components:{  vueSeamlessScroll}  //template  <vue-seamless-scroll:data="Top10GatewayLog":class-option="optionSingleHeigh......
  • 6. Vue.js 表单输入绑定 #yyds干货盘点#【yyds干货盘点】
    学习目录:Vue.js简介Vue.js实例与数据绑定Vue.js计算属性和侦听器Vue.js条件渲染和列表渲染Vue.js事件处理Vue.js表单输入绑定Vue.js组件基础Vue.js组件通信Vue.js插槽Vue.js动态组件和异步组件Vue.js自定义指令Vue.js过渡和动画Vue.js混入Vue.js自定义事件和v-model......
  • vue项目base64编码或者参数里面有 +号问题
    当后端返回的链接的token或者参数里带有加号,浏览器打开链接时,页面通过使用this.$route.query.XXXX获取到的参数里+号会变成空格,导致解密后部分内容变成乱码。解决方法:1.链接是后端返回时,需后端处理,将+号编码变成%2B,再返回链接进行跳转即可2.链接是前端写的话,可直接使用encodeURIC......
  • vue Router的原理及传参方法
    VueRouter是Vue.js官方的路由管理器,它和Vue.js的核心深度集成,可以非常方便地实现单页面应用程序(SPA)的路由功能。VueRouter的原理主要是通过监听URL的变化,根据不同的URL显示不同的组件,从而实现页面的切换和跳转。具体来说,VueRouter的原理包括以下几个方面:路由配置......
  • 终于理解集线器、交换机、路由器之间的区别了
    集线器、交换机、路由器 什么是集线器Hub?1、把内网中的网络设备连接起来,支持多个以太网连接的端口,可以连接多种网络设备2、仅仅知道端口上是否连接了设备,经过集线器传输的数据包,所有设备都能接收到,如下图,当主机A发送数据包给主机C时,主机B和D都能接收到数据3、不仅带来......
  • Qt 事件系统总结
    参考:(35条消息)Qt事件循环及QEventLoop的使用_kupeThinkPoem的博客-CSDN博客(35条消息)Qt消息机制:事件分发和事件过滤_qt消息过滤_SOC罗三炮的博客-CSDN博客Qt事件系统总结Qt事件在Qt中,事件(event)是一些对象,它们都派生自抽象类QEvent事件是应用程序所关心的,......
  • Segment-Anything的一些相关论文总结
    1、SegmentAnythingModel(SAM)EnhancedPseudoLabelsforWeaklySupervisedSemanticSegmentation https://avoid.overfit.cn/post/92f50aa2951d4dd89cfc4fe71e0531ef......
  • Vue考试复习
    App.vue<scriptsetup>importHelloWorldfrom'./components/HelloWorld.vue'importLoginfrom'./components/Login.vue'//importCalcfrom'./components/Calc.vue'//importShoppingCartfrom'./components/ShoppingC......
  • 【C++】STL常用容器总结之十二:string类
    13、string类声明string类本不是STL的容器,但是它与STL容器有着很多相似的操作,因此,把string放在这里一起进行介绍。之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完......
  • 1.基础知识(5) --Matlab中特殊符号使用总结
    ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。......