• vue之生命周期与导航守卫


    组件钩子函数:

    beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeDestroy、destoryed

    还有两个特殊的(使用keep-alive):activated、deactivated(不详述)

    v2.5.0+新增: errorCaptured (暂时还不知道咋用)

    路由守卫:

    全局&路由独享:

    beforeEach、beforeResolve(v2.5.0+新增)、afterEach ;beforeEnter(路由独享,类似beforeEach)

    组件内:

    beforeRouteEnter、beforeRouteUpdate (2.2 新增)、beforeRouteLeave

    代码示例:

    //Router定义
    
    Vue.use(Router)
    
    const router = new Router({
        ...
    })
    
    //导航守卫
    
    router.beforeEach((to, from, next) => {
      console.log("导航前置守卫: beforeEach,");
      next();
    })
    router.afterEach((to, from) => {
      console.log("导航后置守卫: afterEach,");
    }) 
    router.beforeResolve ((to, from, next) => {
      console.log("导航解析守卫: beforeResolve,");
      next();
    }) 

    组件钩子:

    export default {
      //钩子
      beforeCreate(){
        console.log("组件钩子: beforeCreate");
      },
      created(){
        console.log("组件钩子: created");
      },
      beforeMount(){
        console.log("组件钩子: beforeMount");
      },
      mounted(){
        console.log("组件钩子: mounted");
      },
      beforeUpdate(){
        console.log("组件钩子: beforeUpdate");
      },
      updated(){
        console.log("组件钩子: updated");
      },
      beforeDestroy(){
        console.log("组件钩子: beforeDestroy");
      },
      destoryed(){
        console.log("组件钩子: destoryed");
      },
      beforeRouteEnter (to, from, next) {
        console.log("组件内部守卫: beforeRouteEnter,");
        next()
      },
      beforeRouteUpdate (to, from, next) {
        console.log("组件内部守卫: beforeRouteUpdate,");
        next()
      },
      beforeRouteLeave (to, from, next) {
        console.log("组件内部守卫: beforeRouteLeave,");
        next()
      }
    }

    执行输出顺序:

        导航前置守卫: beforeEach
        组件内部守卫: beforeRouteEnter
        导航解析守卫: beforeResolve
        导航后置守卫: afterEach
        组件钩子: beforeCreate
        组件钩子: created
        组件钩子: beforeMount
        组件钩子: mounted
        //执行跳转
        组件内部守卫: beforeRouteLeave
        导航前置守卫: beforeEach
        导航解析守卫: beforeResolve
        导航后置守卫: afterEach
        组件钩子: beforeDestroy
  • 相关阅读:
    Windows 7 Phone 文档数据库Rapid Repository正式发布
    Adobe展示HTML5动画制作IDE
    详解Android实现全屏正确方法
    qtform.com计划
    Adobe加速布局移动开发:Flash Builder+Flex+AIR+Catalyst
    预览:Visual Basic与C#中的异步语法
    Windows 7主题中的壁纸从哪里来?
    F#的编译器及标准库使用Apache 2.0协议开源(暂时还没有看到未来)
    开发者谈Symbian、iPhone、Android、MeeGo平台
    MeeGo 1.1发布
  • 原文地址:https://www.cnblogs.com/fanlu/p/10825275.html
Copyright © 2020-2023  润新知