[{"data":1,"prerenderedAt":714},["ShallowReactive",2],{"page-\u002Fvue\u002Fvuerouter导航守卫":3},{"id":4,"title":5,"body":6,"description":707,"extension":708,"meta":709,"navigation":247,"path":710,"seo":711,"stem":712,"__hash__":713},"content\u002FVue\u002FVueRouter导航守卫.md","VueRouter导航守卫",{"type":7,"value":8,"toc":688},"minimark",[9,13,21,25,62,66,69,210,213,216,219,225,270,273,288,315,318,324,350,353,359,412,415,418,421,436,471,474,505,535,538,547,577,580,664,667,675,678,684],[10,11,5],"h1",{"id":12},"vuerouter导航守卫",[14,15,16,20],"p",{},[17,18,19],"code",{},"vue-router","提供的导航守卫主要用来通过跳转或取消的方式守卫导航，简单来说导航守卫就是路由跳转过程中的一些钩子函数，路由跳转是一个大的过程，这个大的过程分为跳转前中后等等细小的过程，而在每一个过程中都有钩子函数，这些钩子函数能使我们在这些过程中进行一些操作，这就是导航守卫。",[22,23,24],"h2",{"id":24},"描述",[14,26,27,29,30,33,34,33,37,40,41,44,45,33,48,33,51,54,55,58,59,61],{},[17,28,19],{},"一套钩子来触发路由在不同阶段触发的函数，导航守卫分成三大块：全局守卫、路由独享守卫和组件内守卫。全局守卫顾名思义所有路由在进入跳转的时候都会触发，整个全局路由分为三个阶段依次是",[17,31,32],{},"beforeEach","、",[17,35,36],{},"beforeResolve",[17,38,39],{},"afterEach","。路由独享守卫顾名思义只在定义路由和路由组件中的对象中使用，其只有一个阶段",[17,42,43],{},"beforeEnter","。组件内守卫是只在组件中触发的路由内容，其有三个阶段依次是",[17,46,47],{},"beforeRouteEnter",[17,49,50],{},"beforeRouteUpdate",[17,52,53],{},"beforeRouteLeave","。参数或查询的改变并不会触发进入或离开的导航守卫，可以通过观察",[17,56,57],{},"$route","对象来应对这些变化，或使用",[17,60,50],{},"的组件内守卫。",[63,64,65],"h3",{"id":65},"参数",[14,67,68],{},"通常导航守卫接收三个参数，当然并不是所有钩子函数都是如此，不接收相关参数的钩子函数会特别说明。",[70,71,72,83,89],"ul",{},[73,74,75,78,79,82],"li",{},[17,76,77],{},"to: Route",": 即将要进入的目标路由对象，即组件内的",[17,80,81],{},"this.$route","。",[73,84,85,88],{},[17,86,87],{},"from: Route",": 当前导航正要离开的路由对象。",[73,90,91,94,95,98,99,102,103,105,106],{},[17,92,93],{},"next: Function",": 一定要调用该方法来",[17,96,97],{},"resolve","这个钩子，需要确保",[17,100,101],{},"next","函数在任何给定的导航守卫中都被严格调用一次，其可以出现多于一次，但是只能在所有的逻辑路径都不重叠的情况下，否则钩子永远都不会被解析或报错，此函数执行效果依赖",[17,104,101],{},"方法的调用参数。\n",[70,107,108,118,135,167,193],{},[73,109,110,113,114,117],{},[17,111,112],{},"next()",": 进行管道中的下一个钩子，如果全部钩子执行完了，则导航的状态就是确认",[17,115,116],{},"confirmed","的。",[73,119,120,123,124,127,128,130,131,134],{},[17,121,122],{},"next(false)",": 中断当前的导航，如果浏览器的",[17,125,126],{},"URL","改变了，例如用户手动或者浏览器后退按钮，那么",[17,129,126],{},"地址会重置到",[17,132,133],{},"from","路由对应的地址。",[73,136,137,140,141,144,145,147,148,33,151,154,155,158,159,162,163,166],{},[17,138,139],{},"next(\"\u002F\")","或者",[17,142,143],{},"next({ path: \"\u002F\" })",": 跳转到一个不同的地址，当前的导航被中断，然后进行一个新的导航，可以向",[17,146,101],{},"传递任位置对象，且允许设置诸如",[17,149,150],{},"replace: true",[17,152,153],{},"name: \"home\"","之类的选项以及任何用在",[17,156,157],{},"router-link","的",[17,160,161],{},"to prop","或",[17,164,165],{},"router.push","中的选项。",[73,168,169,172,173,175,176,178,179,182,183,185,186,188,189,192],{},[17,170,171],{},"next((vm)=>{})",": ",[17,174,47],{},"是支持给",[17,177,101],{},"传递回调的唯一守卫，回调内接收的参数为当前组件的",[17,180,181],{},"vm","，对于",[17,184,50],{},"和",[17,187,53],{},"来说，",[17,190,191],{},"this","已经可用了，所以无需也不支持传递回调。",[73,194,195,198,199,201,202,205,206,209],{},[17,196,197],{},"next(error) (2.4.0+)",": 如果传入",[17,200,101],{},"的参数是一个",[17,203,204],{},"Error","实例，则导航会被终止且该错误会被传递给",[17,207,208],{},"router.onError()","注册过的回调。",[22,211,212],{"id":212},"全局守卫",[14,214,215],{},"全局守卫是用来监测所有的路由，通常直接在定义路由时构建。",[63,217,218],{"id":218},"全局前置守卫",[14,220,221,222,224],{},"全局前置守卫在路由刚开始导航且还未进入路由对应的组件中时触发，简单来说即最早触发，但是触发时候没有任何组件等加载，正因为如此适合做登陆判断逻辑。当一个导航触发时，全局前置守卫按照创建顺序调用，守卫是异步解析执行，此时导航在所有守卫",[17,223,97],{},"完之前一直处于等待中。",[226,227,232],"pre",{"className":228,"code":229,"language":230,"meta":231,"style":231},"language-javascript shiki shiki-themes github-light github-dark","const router = new VueRouter({ \u002F*...*\u002F })\n\nrouter.beforeEach((to, from, next) => {\n  \u002F\u002F ... \u002F\u002F if (...) next(); else next(\"\u002Flogin\");\n})\n","javascript","",[17,233,234,242,249,255,264],{"__ignoreMap":231},[235,236,239],"span",{"class":237,"line":238},"line",1,[235,240,241],{},"const router = new VueRouter({ \u002F*...*\u002F })\n",[235,243,245],{"class":237,"line":244},2,[235,246,248],{"emptyLinePlaceholder":247},true,"\n",[235,250,252],{"class":237,"line":251},3,[235,253,254],{},"router.beforeEach((to, from, next) => {\n",[235,256,258,261],{"class":237,"line":257},4,[235,259,260],{},"  \u002F\u002F ...",[235,262,263],{}," \u002F\u002F if (...) next(); else next(\"\u002Flogin\");\n",[235,265,267],{"class":237,"line":266},5,[235,268,269],{},"})\n",[63,271,272],{"id":272},"全局解析守卫",[14,274,275,276,279,280,283,284,287],{},"在",[17,277,278],{},"2.5.0+","可以使用",[17,281,282],{},"router.beforeResolve","注册一个全局守卫，这和",[17,285,286],{},"router.beforeEach","类似，区别是在导航被确认之前，同时在所有组件内守卫和异步路由组件被解析之后，解析守卫才被调用。",[226,289,291],{"className":228,"code":290,"language":230,"meta":231,"style":231},"const router = new VueRouter({ \u002F*...*\u002F })\n\nrouter.beforeResolve((to, from, next) => {\n  \u002F\u002F ... \n})\n",[17,292,293,297,301,306,311],{"__ignoreMap":231},[235,294,295],{"class":237,"line":238},[235,296,241],{},[235,298,299],{"class":237,"line":244},[235,300,248],{"emptyLinePlaceholder":247},[235,302,303],{"class":237,"line":251},[235,304,305],{},"router.beforeResolve((to, from, next) => {\n",[235,307,308],{"class":237,"line":257},[235,309,310],{},"  \u002F\u002F ... \n",[235,312,313],{"class":237,"line":266},[235,314,269],{},[63,316,317],{"id":317},"全局后置钩子",[14,319,320,321,323],{},"注册全局后置钩子，和守卫不同的是，这些钩子不会接受",[17,322,101],{},"函数也不会改变导航本身。",[226,325,327],{"className":228,"code":326,"language":230,"meta":231,"style":231},"const router = new VueRouter({ \u002F*...*\u002F })\n\nrouter.afterEach((to, from) => {\n  \u002F\u002F ... \n})\n",[17,328,329,333,337,342,346],{"__ignoreMap":231},[235,330,331],{"class":237,"line":238},[235,332,241],{},[235,334,335],{"class":237,"line":244},[235,336,248],{"emptyLinePlaceholder":247},[235,338,339],{"class":237,"line":251},[235,340,341],{},"router.afterEach((to, from) => {\n",[235,343,344],{"class":237,"line":257},[235,345,310],{},[235,347,348],{"class":237,"line":266},[235,349,269],{},[22,351,352],{"id":352},"路由独享守卫",[14,354,355,356,358],{},"路由独享守卫中只有",[17,357,43],{},"阶段，是当前组件路由进入的时候触发的阶段。",[226,360,362],{"className":228,"code":361,"language":230,"meta":231,"style":231},"const router = new VueRouter({\n  routes: [{\n      path: \"\u002Fexample\",\n      component: Example,\n      beforeEnter: (to, from, next) => {\n        \u002F\u002F ...\n      }\n    }]\n})\n",[17,363,364,369,374,379,384,389,395,401,407],{"__ignoreMap":231},[235,365,366],{"class":237,"line":238},[235,367,368],{},"const router = new VueRouter({\n",[235,370,371],{"class":237,"line":244},[235,372,373],{},"  routes: [{\n",[235,375,376],{"class":237,"line":251},[235,377,378],{},"      path: \"\u002Fexample\",\n",[235,380,381],{"class":237,"line":257},[235,382,383],{},"      component: Example,\n",[235,385,386],{"class":237,"line":266},[235,387,388],{},"      beforeEnter: (to, from, next) => {\n",[235,390,392],{"class":237,"line":391},6,[235,393,394],{},"        \u002F\u002F ...\n",[235,396,398],{"class":237,"line":397},7,[235,399,400],{},"      }\n",[235,402,404],{"class":237,"line":403},8,[235,405,406],{},"    }]\n",[235,408,410],{"class":237,"line":409},9,[235,411,269],{},[22,413,414],{"id":414},"组件内的守卫",[14,416,417],{},"可以在路由组件内直接定义导航守卫。",[63,419,420],{"id":420},"组件前置守卫",[14,422,423,424,427,428,430,431,433,434,82],{},"在还没有进入该组件之前触发，在渲染该组件的对应路由被",[17,425,426],{},"confirm","前调用，此时不能获取组件实例 ",[17,429,191],{},"，因为当守卫执行前，组件实例还没被创建，但是可以通过传一个回调给",[17,432,101],{},"来访问组件实例，在导航被确认的时候执行回调，并且把组件实例作为回调方法的参数，即上文参数中提到的",[17,435,171],{},[226,437,439],{"className":228,"code":438,"language":230,"meta":231,"style":231},"const Example = {\n  template: `...`,\n  beforeRouteEnter: function(to, from, next) {\n    \u002F\u002F ...\n  }\n}\n",[17,440,441,446,451,456,461,466],{"__ignoreMap":231},[235,442,443],{"class":237,"line":238},[235,444,445],{},"const Example = {\n",[235,447,448],{"class":237,"line":244},[235,449,450],{},"  template: `...`,\n",[235,452,453],{"class":237,"line":251},[235,454,455],{},"  beforeRouteEnter: function(to, from, next) {\n",[235,457,458],{"class":237,"line":257},[235,459,460],{},"    \u002F\u002F ...\n",[235,462,463],{"class":237,"line":266},[235,464,465],{},"  }\n",[235,467,468],{"class":237,"line":391},[235,469,470],{},"}\n",[63,472,473],{"id":473},"组件更新守卫",[14,475,476,477,480,481,484,485,487,488,491,492,185,495,498,499,502,503,82],{},"组件更新守卫在动态路由中使用，由于动态路由中切换路由的时候，由于绑定的是同一个组件因此在不会在重新渲染，但是为了可以让组件中的内容重新渲染，有两种方法第一种使用",[17,478,479],{},"watch","监听，这种需要使用",[17,482,483],{},"props","写法，另一种就是在",[17,486,50],{},"中定义，其会监听到动态路由的改变，因此可以在这个钩子中获取异步动态路由对应的数据，举例来说，对于一个带有动态参数的路径",[17,489,490],{},"\u002Fexample\u002F:id","，在",[17,493,494],{},"\u002Fexample\u002F1",[17,496,497],{},"\u002Fexample\u002F2","之间跳转的时候，由于会渲染同样的",[17,500,501],{},"Example","组件，因此组件实例会被复用，而这个钩子就会在这个情况下被调用，在这个钩子函数中可以访问组件实例 ",[17,504,191],{},[226,506,508],{"className":228,"code":507,"language":230,"meta":231,"style":231},"const Example = {\n  template: `...`,\n  beforeRouteUpdate: function(to, from, next) {\n    \u002F\u002F ...\n  }\n}\n",[17,509,510,514,518,523,527,531],{"__ignoreMap":231},[235,511,512],{"class":237,"line":238},[235,513,445],{},[235,515,516],{"class":237,"line":244},[235,517,450],{},[235,519,520],{"class":237,"line":251},[235,521,522],{},"  beforeRouteUpdate: function(to, from, next) {\n",[235,524,525],{"class":237,"line":257},[235,526,460],{},[235,528,529],{"class":237,"line":266},[235,530,465],{},[235,532,533],{"class":237,"line":391},[235,534,470],{},[63,536,537],{"id":537},"组件离开守卫",[14,539,540,541,543,544,546],{},"导航离开该组件的对应路由时调用，可以访问组件实例",[17,542,191],{},"，这个离开守卫通常用来禁止用户在还未保存修改前突然离开，该导航可以通过",[17,545,122],{},"来取消。",[226,548,550],{"className":228,"code":549,"language":230,"meta":231,"style":231},"const Example = {\n  template: `...`,\n  beforeRouteLeave: function(to, from, next) {\n    \u002F\u002F ...\n  }\n}\n",[17,551,552,556,560,565,569,573],{"__ignoreMap":231},[235,553,554],{"class":237,"line":238},[235,555,445],{},[235,557,558],{"class":237,"line":244},[235,559,450],{},[235,561,562],{"class":237,"line":251},[235,563,564],{},"  beforeRouteLeave: function(to, from, next) {\n",[235,566,567],{"class":237,"line":257},[235,568,460],{},[235,570,571],{"class":237,"line":266},[235,572,465],{},[235,574,575],{"class":237,"line":391},[235,576,470],{},[22,578,579],{"id":579},"导航解析流程",[70,581,582,585,591,596,605,610,613,618,625,628,633,648,655],{},[73,583,584],{},"导航被触发。",[73,586,587,588,590],{},"在失活的组件里调用",[17,589,53],{},"守卫。",[73,592,593,594,590],{},"调用全局的",[17,595,32],{},[73,597,598,599,601,602,82],{},"在重用的组件里调用",[17,600,50],{},"守卫",[17,603,604],{},"2.2+",[73,606,607,608,82],{},"在路由配置里调用",[17,609,43],{},[73,611,612],{},"解析异步路由组件。",[73,614,615,616,82],{},"在被激活的组件里调用",[17,617,47],{},[73,619,593,620,601,622,82],{},[17,621,36],{},[17,623,624],{},"2.5+",[73,626,627],{},"导航被确认。",[73,629,593,630,632],{},[17,631,39],{},"钩子。",[73,634,635,636,33,639,33,642,33,645,82],{},"组件生命周期",[17,637,638],{},"beforeCreate",[17,640,641],{},"created",[17,643,644],{},"beforeMount",[17,646,647],{},"mounted",[73,649,650,651,654],{},"触发",[17,652,653],{},"DOM","更新。",[73,656,657,658,660,661,663],{},"调用",[17,659,47],{},"守卫中传给",[17,662,101],{},"的回调函数，创建好的组件实例会作为回调函数的参数传入。",[22,665,666],{"id":666},"每日一题",[226,668,673],{"className":669,"code":671,"language":672},[670],"language-text","https:\u002F\u002Fgithub.com\u002FWindrunnerMax\u002FEveryDay\n","text",[17,674,671],{"__ignoreMap":231},[22,676,677],{"id":677},"参考",[226,679,682],{"className":680,"code":681,"language":672},[670],"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fp\u002F54112006\nhttps:\u002F\u002Fjuejin.im\u002Fpost\u002F6844903652272898056\nhttps:\u002F\u002Fsegmentfault.com\u002Fa\u002F1190000016571559\nhttp:\u002F\u002Fstatic.kancloud.cn\u002Fcyyspring\u002Fvuejs\u002F941801\nhttps:\u002F\u002Fwww.cnblogs.com\u002Fkzxiaotan\u002Fp\u002F11676872.html\nhttps:\u002F\u002Frouter.vuejs.org\u002Fzh\u002Fguide\u002Fadvanced\u002Fnavigation-guards.html\n",[17,683,681],{"__ignoreMap":231},[685,686,687],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":231,"searchDepth":244,"depth":244,"links":689},[690,693,698,699,704,705,706],{"id":24,"depth":244,"text":24,"children":691},[692],{"id":65,"depth":251,"text":65},{"id":212,"depth":244,"text":212,"children":694},[695,696,697],{"id":218,"depth":251,"text":218},{"id":272,"depth":251,"text":272},{"id":317,"depth":251,"text":317},{"id":352,"depth":244,"text":352},{"id":414,"depth":244,"text":414,"children":700},[701,702,703],{"id":420,"depth":251,"text":420},{"id":473,"depth":251,"text":473},{"id":537,"depth":251,"text":537},{"id":579,"depth":244,"text":579},{"id":666,"depth":244,"text":666},{"id":677,"depth":244,"text":677},"vue-router提供的导航守卫主要用来通过跳转或取消的方式守卫导航，简单来说导航守卫就是路由跳转过程中的一些钩子函数，路由跳转是一个大的过程，这个大的过程分为跳转前中后等等细小的过程，而在每一个过程中都有钩子函数，这些钩子函数能使我们在这些过程中进行一些操作，这就是导航守卫。","md",{},"\u002Fvue\u002Fvuerouter导航守卫",{"title":5,"description":707},"Vue\u002FVueRouter导航守卫","TzZmwpWgFZRLbvwTKEhOSgNCkCWOdQtWFSW7-_xVdh0",1791042468765]