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

当前文章收录状态:
未收录

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

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

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

解决方法:在next中调用

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

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

 

© 版权声明
THE END
我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=270198dipw4ko
点赞11赞赏 分享
Death comes to all, but great achievements raise a monument which shall endure until the sun grows old.
死亡无人能免,但非凡的成就会树起一座纪念碑,它将一直立到太阳冷却之时
评论 抢沙发

请登录后发表评论

    暂无评论内容