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赞赏 分享
No matter what label is thrown your way, only you can define your self.
不管你被贴上什么标签,只有你才能定义你自己
评论 抢沙发

请登录后发表评论

    暂无评论内容