vue router生命周期

vue router生命周期

router中的参数

router异步解析执行,此时router在 resolve 完之前一直处于 等待中

三个参数:

  • to: Route: 即将要进入的目标 路由对象

  • from: Route: 当前导航正要离开的路由

  • next: Function: 一定要调用该方法来 resolve 这个钩子

组件中路由的生命周期中的不同方法:
beforeRouteUpdate(to, from, next) {
    this.type = to.params.type;
    this.onPullingDown();
  }

组件中路由的生命周期中的不同方法:



beforeRouteEnter (to, from, next) {
    // 在渲染该组件的对应路由被 confirm 前调用
    // 不!能!获取组件实例 `this`
    // 因为当守卫执行前,组件实例还没被创建
  },
    
  // 对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候,由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
  beforeRouteUpdate (to, from, next) {
    // 在当前路由改变,但是该组件被复用时调用
    // 可以访问组件实例 `this`
  },
    
  // 这个离开守卫通常用来禁止用户在还未保存修改前突然离开。该导航可以通过 next(false) 来取消。
  beforeRouteLeave (to, from, next) {
    // 导航离开该组件的对应路由时调用
    // 可以访问组件实例 `this`
  }

Vue中生命周期总结

  1. 根组件实例:8个 (beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeDestroy、destroyed)

  2. 组件实例:8个 (beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeDestroy、destroyed)

  3. 全局路由钩子:2个 (beforeEach、afterEach)

  4. 组件路由钩子:3个 (beforeRouteEnter、beforeRouteUpdate、beforeRouteLeave)

  5. 指令的周期: 5个 (bind、inserted、update、componentUpdated、unbind)

  6. beforeRouteEnter的next所对应的周期

  7. nextTick所对应的周期

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Router生命周期包括全局路由钩子和组件路由钩子。 全局路由钩子是在整个应用的路由切换过程中触发的钩子函数。其中,beforeEach函数在每次路由切换前被调用,可以用来进行全局的路由守卫逻辑。\[1\] 组件路由钩子是在组件的路由切换过程中触发的钩子函数。其中,beforeRouteEnter函数在渲染组件的对应路由被验证前调用,但此时无法访问组件实例。可以通过next函数获取组件实例,并在回调函数中访问组件实例。beforeRouteUpdate函数在当前路由改变但组件被复用时调用,可以访问组件实例。beforeRouteLeave函数在导航离开渲染组件的对应路由时调用,也可以访问组件实例。\[2\] 需要注意的是,在beforeRouteEnter函数内部,this是undefined,因为组件还没有被创建出来。可以通过next函数的回调参数获取组件的实例对象。\[3\] 综上所述,Vue Router生命周期包括全局路由钩子和组件路由钩子,可以用来进行路由守卫逻辑和访问组件实例。 #### 引用[.reference_title] - *1* *3* [vue生命周期总结](https://blog.csdn.net/weixin_42823014/article/details/123848965)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [VUE 生命周期大全,包含路由器生命周期,单组件生命周期,父子组件嵌套生命周期](https://blog.csdn.net/weixin_43221910/article/details/123228541)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值