Vue的beforeRouteEnter如何使用组件实例的方法

beforeRouteEnter如何使用组件实例的方法

beforeRouteEnter(to, from, next) {
     this.axios()
 }

对于这样的代码,会报错,因为此时组件实例不存在,没有this,就更没有挂载到上面的axios方法了。

解决方法:在next中调用

next可以传入一个参数vm,这个参数表示组件实例,可以用vm代替this使用组件实例的方法了。

beforeRouteEnter(to, from, next) {
   next((vm) => {
       vm.axios()
   }
}

 

© 版权声明
THE END
我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=270198dipw4ko
点赞11赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容