学无先后,达者为师

网站首页 编程语言 正文

组件内路由守卫beforeRouteEnter和beforeRouteLeave

作者:顽强的小豆子 更新时间: 2022-07-10 编程语言
从home.vue组件跳转到news.vue组件调用beforeRouteEnter,离开news.vue组件调用beforeRouteLeave
<template>
  <ul>
    <li>news001 <input type="text"></li>
    <li>news002 <input type="text"></li>
    <li>news003 <input type="text"></li>
  </ul>
</template>


<script>
  export default {
    name:'News',
    data() {
      return {
      }
    },
    //进入该组件进行拦截
    beforeRouteEnter(to,from,next){
      if(to.meta.isAuth){
        if(localStorage.getItem("username") === "yaomm"){
          next()
        }else{
          next("/login")
        }
      }else{
        next()
      }
    },
    //离开news页面时执行
    beforeRouteLeave(to, from, next){
      console.log("守卫完成",to)
      //如果没有next()无法离开news页面
      next()
    }
  }
</script>

原文链接:https://blog.csdn.net/qiuyushuofeng/article/details/124846813

栏目分类
最近更新