vue Route参数传递和导航守卫

路由的传递参数

在这里插入图片描述

对于params方式的传递参数,我们已经讲过。现在我们来使用第二种参数方式来进行练习:

  1. 新建Profile.vue组件

    <template>
        <div>
            <h3>通过query来获取 参数 数据</h3>
            <h5>
                {{$route.query}}
            </h5>
        </div>
    </template>
    <script>
    export default {
      name: 'profile',
      data(){
          
      }
    }
    </script>
    <style  scoped>
    
    </style>
    
    
  2. 去route的index.js中添加路径

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    // import Home from '../components/HelloWorld.vue'
    // import about from '../components/about.vue'
    // import user from '../components/test.vue'
    //先使用 vue的uuse来使用vueroute
    Vue.use(VueRouter)
    //定义route路由
      const routes = [
        {
          path: '/',
          name:'first',
          redirect: '/pagehome'
        },
        {
          path: '/profile',
          component: () => import('../components/Profile.vue')
        },
      {
        path: '/pagehome',
        name: '首页',
        component: () => import('../components/HelloWorld.vue')
      },
      // {
      //   path: '/about',
      //   name: 'About',
      //   // route level code-splitting
      //   // this generates a separate chunk (about.[hash].js) for this route
      //   // which is lazy-loaded when the route is visited.
      //   路由懒加载
      //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
      // }
      {
        path: '/about',
        name: '关于',
        component: () => import('../components/about.vue'),
        children:[
          {
            path: 'aboutSub',
            component: () => import ('../components/test2.vue')
          }
        ]
    
      },
      {
        path: '/user/:userId',
        component: () => import('../components/test.vue')
      }
      
    ]
    //history模式就是我们学的history的go,reward。pushState。replaceState啊等。
    const router = new VueRouter({
      mode: 'history',
      base: process.env.BASE_URL,
      routes,
      activeClass: 'changecolor'
    })
    //导出route组件
    export default router
    
    
  3. 添加routelink

    <template>
      <div id="app">
        <div id="nav">
          <!-- <router-link to="/pageHome" tag="button" active-class="changecolor">Home</router-link> |
          <router-link to="/about" tag="button" active-class="changecolor">About</router-link> -->
          <!-- <button>显示主页</button>
          <button>显示about页面</button> -->
          <!-- <button @click="gohome">首页</button>
          <button @click="goabout">about</button> -->
          <!-- <router-link to="/pagehome">首页</router-link>  -->
          <!-- <router-link to="/about">关于</router-link>  -->
          <!-- <router-link :to="'/user/'+userid">用户界面</router-link>  -->
          <!-- <router-link>新组件</router-link>  -->
          <router-link :to="{
            path: '/profile',
            query:{
              name: '里巴巴'
              ,age: 22
              ,sex: 1
            }
          }">个人信息</router-link>
        </div>
        <router-view/>
    
      </div>
    </template>
    <script>
    export default {
        name: 'App'
        ,data(){
          return {
            userid: 'zhangsan1'
          }
        }
    }
    </script>
    <style>
      .changecolor{
        color:red;
      }
    </style>
    
    
  4. 运行效果

在这里插入图片描述

R o u t e 和 R o u t e r 的 区 别 Route和Router的区别 RouteRouter

R o u t e 是 处 于 活 跃 的 路 由 , R o u t e r 是 v u e 使 用 的 模 块 。 具 体 还 得 网 上 找 文 章 对 源 码 进 行 分 析 Route是处于活跃的路由,Router是vue使用的模块。具体还得网上找文章对源码进行分析 RouteRoutervue使

导航守卫

​ 俗称:对来回跳转过程进行监听。就是监听用户点击什么路径,从A跳到B。

案例 点击组件,浏览器需要更新对应页面标题

重点在于代码末尾的 - -使用全局导航守卫。

路由对象中name和meta可以两个选一个,在这里,我选route的name属性。

import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../components/HelloWorld.vue'
// import about from '../components/about.vue'
// import user from '../components/test.vue'
//先使用 vue的uuse来使用vueroute
Vue.use(VueRouter)
//定义route路由
  const routes = [
    {
      path: '/',
      redirect: '/pagehome'
    },
    {
      path: '/profile',
      name: '个人简介',
      component: () => import('../components/Profile.vue')
      ,meta: { 
        title: '个人简介'
       }
    },
  {
    path: '/pagehome',
    name: '首页',
    component: () => import('../components/HelloWorld.vue')
    ,meta: { 
      title: '首页'
     }
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   路由懒加载
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
  {
    path: '/about',
    name: '关于',
    component: () => import('../components/about.vue'),
    meta: { 
      title: '关于'
     },
    children:[
      {
        path: 'aboutSub',
        component: () => import ('../components/test2.vue')
        ,meta: { 
          title: '关于的子组件'
         }
      }
    ]

  },
  {
    path: '/user/:userId',
    name: '个人中心',
    
    component: () => import('../components/test.vue')
    ,meta: { 
      title: '个人中心'
     }
  }
  
]
//history模式就是我们学的history的go,reward。pushState。replaceState啊等。
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  activeClass: 'changecolor'
})
//使用全局导航守卫
router.beforeEach((to,from,next)=>{
  document.title = to.name;
  next()

})
//导出route组件
export default router

运行结果如下:
在这里插入图片描述

方法2:使用match的数组对象来进行改变值:

//使用全局导航守卫
router.beforeEach((to,from,next)=>{
  // document.title = to.name;
    //使用方法2,从匹配对象中进行获取其标题值
  document.title = to.matched[0].meta.title;
  next()
})

运行效果同上。

在这里插入图片描述

导航守卫可以配置类似拦截器的功能。可以使用前置钩子函数后置钩子函数进行登入或者权限校验的判断。

使用keep-alive对组件不进行销毁,下面假设先不使用keep-alive进行查看情况,看组件会不会销毁:
在这里插入图片描述

可以看到,当点击其他route时,组件会被销毁。

在加入keep-alive后,app.vue代码如下:

<template>
  <div id="app">
    <div id="nav">
      <!-- <router-link to="/pageHome" tag="button" active-class="changecolor">Home</router-link> |
      <router-link to="/about" tag="button" active-class="changecolor">About</router-link> -->
      <!-- <button>显示主页</button>
      <button>显示about页面</button> -->
      <!-- <button @click="gohome">首页</button>
      <button @click="goabout">about</button> -->
      <!-- <router-link to="/pagehome">首页</router-link>  -->
      <!-- <router-link to="/about">关于</router-link>  -->
      <!-- <router-link :to="'/user/'+userid">用户界面</router-link>  -->
      <!-- <router-link>新组件</router-link>  -->

        <router-link :to="{
        path: '/profile',
        query:{
          name: '里巴巴'
          ,age: 22
          ,sex: 1
        }
      }">个人信息</router-link>
    
    </div>
    <keep-alive>
      <router-view/>
    </keep-alive>
  </div>
</template>
<script>
export default {
    name: 'App'
    ,data(){
      return {
        userid: 'zhangsan1'
      }
    }
}
</script>
<style>
  .changecolor{
    color:red;
  }
</style>

运行效果如下:

在这里插入图片描述

可以看到,首页组件保持了活跃状态。

keep-alive常用属性:

在这里插入图片描述

指定一些组件不缓存或者缓存,使用:

修改profile组件代码:

<template>
    <div>
        <h3>通过query来获取 参数 数据</h3>
        <h5>
            {{$route.query}}
        </h5>
    </div>
</template>
<script>
export default {
  name: 'profile'
  ,beforeCreate () {
    console.log("proifle被创建")
  } 
  ,beforeDestroy () {
    console.log("profile组件被销毁")
  }
}
</script>
<style  scoped>

</style>

修改app.vue

<template>
  <div id="app">
    <div id="nav">
      <!-- <router-link to="/pageHome" tag="button" active-class="changecolor">Home</router-link> |
      <router-link to="/about" tag="button" active-class="changecolor">About</router-link> -->
      <!-- <button>显示主页</button>
      <button>显示about页面</button> -->
      <!-- <button @click="gohome">首页</button>
      <button @click="goabout">about</button> -->
      <!-- <router-link to="/pagehome">首页</router-link>  -->
      <!-- <router-link to="/about">关于</router-link>  -->
      <!-- <router-link :to="'/user/'+userid">用户界面</router-link>  -->
      <!-- <router-link>新组件</router-link>  -->

        <router-link :to="{
        path: '/profile',
        query:{
          name: '里巴巴'
          ,age: 22
          ,sex: 1
        }
      }">个人信息</router-link>
    
    </div>
    <keep-alive exclude="profile">
      <router-view/>
    </keep-alive>
  </div>
</template>
<script>
export default {
    name: 'App'
    ,data(){
      return {
        userid: 'zhangsan1'
      }
    }
}
</script>
<style>
  .changecolor{
    color:red;
  }
</style>

运行:

在这里插入图片描述

可以看到,虽然我们使用了keep-alive,但是已经将profile组件排除在外了,不对此组件进行缓存。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值