路由单独钩子(router独享守卫)

路由单独钩子(router独享守卫)

使用:在路由配置中单独加入钩子,在src/router/index.js中使用,代码如下:

1、创建one.vue组件:

<div>
        <h1>我是首页组件中的 子组件 -- one.vue</h1>
    </div>

2、在src/router/index.js中, 在one组件中单独加 钩子:

import one from '@/views/one.vue'

Vue.use(VueRouter)

const routes = [
  // 重定向
  {
    path:'/home', // 从这个地方开始 能够访问到下面的地址
    redirect:'/' // 从上面的 访问到这里
  },
  {
    path: '/', // 访问路径
    name:'home',
    component: HomeView,
    // 嵌套路由 -- 子路由
    children:[
      {
        path:'/home/one',
        component:one,
        // 在路由中 单独添加钩子 -- 路由单独钩子(router独享守卫)
        beforeEnter:(to, from, next)=>{
          console.log('进入前执行');
          next(); // 放行
        }
      }
    ]
  },

3、在HomeView.vue组件中,访问子组件:

<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png">
    <!-- <HelloWorld msg="Welcome to Your Vue.js App"/> -->

    <!-- 路由跳转标签 -->
    <router-link to="/home/one">我要访问 one组件</router-link>
    <!-- 路由占位符 --- 添加后  我们才能在home视图层中看到 one.vue 里的内容 -->
    <router-view></router-view>
  </div>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'HomeView',
  components: {
    // HelloWorld
  }
}
</script>

预览效果:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值