vue-router导航守卫执行顺序

一、测试案例

1.目录结构

在这里插入图片描述

2.bottom.vue

<template>
<div>
bottom<br/>
  <button @click="toIndex1()">go to index1</button>
</div>
</template>

<script>
export default {
  name: "bottom",
  methods:{
    toIndex1(){
      this.$router.push('/index/1');
    }
  },
  beforeRouteEnter(to,from,next){
  console.log("beforeRouteEnter=>进入bottom组件,来自:"+from.meta.ComponentName+"\t去往:"+to.meta.ComponentName);
  next();
},
beforeRouteUpdate(to,from,next){
  console.log("beforeRouteUpdate=>更新bottom组件,来自:"+from.meta.ComponentName+"\t去往:"+to.meta.ComponentName);
  next();
},
beforeRouteLeave(to,from,next){
  console.log("beforeRouteLeave=>离开bottom组件,来自:"+from.meta.ComponentName+"\t去往:"+to.meta.ComponentName);
  next();
},
}
</script>

<style scoped>

</style>

3.index.vue

<template>
  <div class="app">
    index{{this.$route.params.id}}<br/>
    <button @click="toId1()">go to Index1</button>
    <button @click="toId2()">go to Index2</button>
    <button @click="toBottom()">go to bottom</button>

  </div>


</template>

<script>

export default {
  name: 'index',
  beforeRouteEnter(to,from,next){
    console.log("beforeRouteEnter=>进入index组件,来自:"+from.meta.ComponentName+"\t去往:"+to.meta.ComponentName);
    next();
  },
  beforeRouteUpdate(to,from,next){
    console.log("beforeRouteUpdate=>更新index组件,来自:"+from.meta.ComponentName+"\t去往:"+to.meta.ComponentName);
    next();
  },
  beforeRouteLeave(to,from,next){
    console.log("beforeRouteLeave=>离开index组件,来自:"+from.meta.ComponentName+"\t去往:"+to.meta.ComponentName);
    next();
  },
  data() {
    return {
      mathScore: 80,
      englishScore: 60,
    }
  },
  methods:{
    toBottom(){
      this.$router.push('/bottom');
    },
    toId1(){
      this.$router.push('/index/1');
    },
    toId2(){
      this.$router.push('/index/2');
    }
  }

}


</script>


<style scoped>

</style>

4.route.js

import Vue from "vue";
import Router from "vue-router";

Vue.use(Router)

import Index from "../components/index"
import Bottom from '../components/bottom/bottom'
import App from "../App";

let router = new Router({
  // 声明路由的匹配规则
  routes: [
    {
      path: '/',
      component: Index,
      meta: {
        ComponentName: "index",
        keepAlive: true
      },
      beforeEnter(to,from,next){
        console.log("nowRouter...index")
        console.log("beforeEnter=>from:"+from.meta.ComponentName+"\tto:"+to.meta.ComponentName)
        next();
      }
    },
    {
      path: '/index/:id',
      component: Index,
      meta:{
        ComponentName: "index",
        keepAlive: true
      },
      beforeEnter(to,from,next){
        console.log("nowRouter...index")
        console.log("beforeEnter=>from:"+from.meta.ComponentName+"\tto:"+to.meta.ComponentName)
        next();
      }},


    {
      path:'/bottom',
      component: Bottom,
      meta:{
        ComponentName: "bottom",
        keepAlive: true
      },
      beforeEnter(to,from,next){
        console.log("nowRouter...bottom")
        console.log("beforeEnter=>from:"+from.meta.ComponentName+"\tto:"+to.meta.ComponentName)
        next();
      }}

  ],
})



router.beforeEach((to,from,next)=>{
      console.log("beforeEach=>from:"+from.meta.ComponentName+"\tto:"+to.meta.ComponentName)
      next();
})


router.beforeResolve((to, from, next) => {
  console.log("beforeResolve=>from:"+from.meta.ComponentName+"\tto:"+to.meta.ComponentName)
  next();
})

router.afterEach((to, from) => {
  console.log("afterEach=>from:"+from.meta.ComponentName+"\tto:"+to.meta.ComponentName)

})
export default router

5.App.vue

<template>
  <div id="app">
        <keep-alive>
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"></router-view>
  </div>
</template>

<script>
import index from './components'

export default {
  name: 'App',
  components: {
    index
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

6.main.js


import Vue from 'vue'
import App from './App'
import router from './router/router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';



Vue.use(ElementUI);
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

二、结果

1.项目刚启动,首次进入首页/index,

(1.首先执行 全局前置守卫(前置路由为null,前往路由/index) =>
(2.接着执行 路由独享守卫(执行的是要前往的路由/index的路由独享守卫) =>
(3.接着执行 组件内的守卫(beforeRouteEnter,此时index组件还未渲染) =>
(4.接着执行 全局解析守卫 (beforeResolve,在导航被确认之前,在所有组件内守卫 和 异步路由组件 被解析之后)=>
(5.最后执行 全局后置钩子(afterEach)
在这里插入图片描述

2.在首页/index点击路由跳转到/index/1页面

(1.首先执行 组件内守卫(beforeRouteLeave,要离开的index组件的守卫)=>
(2.然后执行 全局前置守卫(前置路由为/index,前往路由/index/1)=>
(3.接着执行 路由独享守卫(执行的是要前往的路由/index/1的路由独享守卫)=>
(4.然后执行 组件内守卫(beforeRouteEnter,此时不属于index组件复用,路由不同,一个是/index,一个是/index:id)=>
(5.接着执行 全局解析守卫 (beforeResolve,在导航被确认之前,在所有组件内守卫 和 异步路由组件 被解析之后)=>
(6.最后执行 全局后置钩子(afterEach)

在这里插入图片描述

3.在/index/1点击路由跳转到/index/2页面

(1.首先执行 全局前置守卫(前置路由为/index/1,前往路由/index/2)=>
(2.然后执行 组件内守卫(beforeRouteUpdate,因为组件index复用,两个都是同一个路由是/index:id)=>
(3.接着执行 全局解析守卫 (beforeResolve,在导航被确认之前,在所有组件内守卫 和 异步路由组件 被解析之后)=>
(4.最后执行 全局后置钩子(afterEach)

在这里插入图片描述

4.在/index/2点击路由跳转到/bottom页面

(1.首先执行 组件内守卫(beforeRouteLeave,要离开的index组件的守卫)=>
(2.然后执行 全局前置守卫(前置路由为/index/2,前往路由/bottom)=>
(3.接着执行 路由独享守卫(执行的是要前往的路由/bottom的路由独享守卫)=>
(4.然后执行 组件内守卫(beforeRouteEnter,此时不属于index组件复用)=>
(5.接着执行 全局解析守卫 (beforeResolve,在导航被确认之前,在所有组件内守卫 和 异步路由组件 被解析之后)=>
(6.最后执行 全局后置钩子(afterEach)

在这里插入图片描述

三、结论

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: vue-router4报错:无效的导航守卫。 在Vue Router4中,导航守卫是一种可以在路由导航发生前、后或者跳转被拒绝时进行控制的方法。当我们在使用导航守卫时,如果出现"invalid navigation guard"的错误提示,可能是由于以下几个原因引起的: 1. 在定义导航守卫时,函数的参数个数错误。在Vue Router4中,导航守卫的函数参数个数是严格规定的,不允许多传或者少传参数。例如,如果我们定义了一个全局前置守卫beforeEach: ``` router.beforeEach((to, from, next) => { // ... }) ``` 那么参数to表示目标路由对象,from表示当前路由对象,next是一个函数,必须调用该函数以确定路由导航的继续。 2. 在给导航守卫传递的参数类型不正确。导航守卫的参数有类型限制,必须按照规定的类型传参。比如beforeEach的参数要求to和from都是RouteLocationNormalized类型,next是NavigationGuardNext类型。 3. 在使用导航守卫时,调用了一个未定义的守卫函数。Vue Router4中的导航守卫有多种类型,如beforeEach、beforeResolve、afterEach等,每种守卫函数都有其特定的用途。如果我们在使用守卫时调用了一个未被Vue Router4定义的守卫函数,就会出现"invalid navigation guard"的错误。 要解决"invalid navigation guard"错误,我们需要仔细检查导航守卫的定义和使用,确保守卫函数参数个数、类型和守卫函数的调用都符合Vue Router4的要求。此外,我们还可以参考Vue Router4的官方文档,对导航守卫有更深入的了解,并按照文档的要求正确地使用守卫函数。 ### 回答2: vue-router4的错误“invalid navigation guard”通常发生在路由导航守卫中出现问题时。 路由导航守卫vue-router中用于控制路由跳转的钩子函数,包括beforeEach、beforeResolve、afterEach等。当路由跳转时,会依次执行各个导航守卫的回调函数。 出现“invalid navigation guard”错误的原因可能有以下几点: 1. 自定义的导航守卫回调函数中出现了错误。 检查自定义导航守卫函数中的语法问题或逻辑错误,如函数调用错误、未定义的变量等。 2. 导航守卫回调函数的参数传递错误。 导航守卫回调函数的参数包括to、from和next,确保正确地传递了这些参数。 3. 导航守卫的使用方式不正确。 在vue-router4中,导航守卫的使用方式与之前版本略有不同。确定你按照新版本的使用方式编写了导航守卫。 4. 导航守卫回调函数中使用了异步操作。 vue-router4中的导航守卫回调函数是同步执行的,不支持使用异步操作。如果在导航守卫中需要进行异步操作,可以使用async/await或Promise。 解决这个错误的方法如下: 1. 检查自定义的导航守卫回调函数中是否存在语法或逻辑错误。 2. 确保正确地传递了导航守卫函数的参数。 3. 检查导航守卫的使用方式是否符合vue-router4的规范。 4. 如有必要,将导航守卫回调函数中的异步操作改为同步操作。 总结 “invalid navigation guard”错误是vue-router4中出现的错误,通常是由于路由导航守卫的问题导致的。检查并修复导航守卫的相关代码可以解决这个错误。 ### 回答3: 在Vue Router 4中,当使用导航守卫时,可能会遇到"invalid navigation guard"的错误。这个错误通常是由于在导航守卫中使用了无效的函数或参数引起的。 在Vue Router 4中,有三种导航守卫:全局前置守卫、路由独享守卫和组件内守卫。当我们使用这些守卫时,需要确保函数或参数的正确性。 首先,要检查导航守卫函数的定义和使用是否正确。全局前置守卫是在路由配置中定义的函数,而路由独享守卫是在路由对象中定义的函数。确保这些函数的名称、参数和返回值符合Vue Router的要求。 其次,要检查导航守卫函数中使用的参数是否正确。导航守卫函数通常接受三个参数: to、 from和next。to表示要导航到的目标路由对象,from表示当前路由对象,next是一个函数,用于控制导航行为。确保这些参数的使用和传递正确无误。 最后,还要检查导航守卫函数的逻辑是否正确。根据具体需求,我们可以在导航守卫中执行一系列的操作,如验证用户权限、重定向到其他路由等。确保这些逻辑的执行顺序和结果符合预期。 总之,在使用Vue Router 4时,如果遇到"invalid navigation guard"的错误,我们需要仔细检查导航守卫函数的定义、参数使用以及逻辑执行,以确保它们的正确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值