vue路由(十二)路由独享守卫beforeEnter和页面内组件路由守卫:beforeRouteEnter,beforeRouteLeave,beforeUpdate

1 路由独享守卫,单个路由拦截处理

 

 {
    path: '/Test',
    name: 'Test',
    components: {
      default: () => import('../views/Test.vue'),
      footerName: Footer
    },
    props: {
      default: route => {
        return route.query.search
      },
      footerName: {
        footerdata: "正版"
      }
    },
    //路由独享守卫
    beforeEnter: (to, from, next) => {
      console.log('000', to, from)
      next()
    }
  },

可以对路由的来去进行,拦截处理

2 页面内路由拦截和处理 写一个test.vue页面

 beforeRouteEnter(to, from, next) {
    console.log(1, to, from);
    console.log(2, this); //此时还没有实例,组件没有被激活
    console.log(3, "此时没有被激活"); //
    next((vm) => {
      console.log("通过回调拿到", vm);
    });
  },

注意此时拿不到this,需要用回调处理。等到页面所有实例加载完成才会出现。

 2  这两个用的不多,可以了解,知道写法用法

beforeRouteLeave(to, from, next) {
    console.log(4, to, from);
    console.log(5, this); //可以拿到,已经被激活
    console.log(6, "此时组件已被被激活"); //
    next();
    //next(false);取消跳转
  },
  beforeUpdate(to, from, next) {
    console.log(7, to, from);
    console.log(8, this); //可以拿到,已经被激活
    console.log(9, "组件改变,路由,参数改变"); //
    next();
  },

beforeRouteLeave

<template>
  <div id="app" style="margin: 50px; line-height: 34px; width: 800px">
    <h3>test:{{ search }}</h3>
    <el-button @click="clickHome">clickHome</el-button>
  </div>
</template>
<script>
export default {
  name: "test",
  props: ["search"],
  beforeRouteEnter(to, from, next) {
    console.log(1, to, from);
    console.log(2, this); //此时还没有实例,组件没有被激活
    console.log(3, "此时没有被激活"); //
    next((vm) => {
      console.log("通过回调拿到", vm);
    });
  },
  beforeRouteLeave(to, from, next) {
    console.log(4, to, from);
    console.log(5, this); //可以拿到,已经被激活
    console.log(6, "此时组件已被被激活"); //
    next();
    //next(false);取消跳转
  },
  beforeUpdate(to, from, next) {
    console.log(7, to, from);
    console.log(8, this); //可以拿到,已经被激活
    console.log(9, "组件改变,路由,参数改变"); //
    next();
  },
  methods: {
    clickHome() {
      this.$router.push({
        path: "/",
      });
    },
  },
};
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小云儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值