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

_

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

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

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

解决方法:在next中调用

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

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

 

nuxt3项目中使用element-plus的el-menu组件default-active无法正常激活菜单 2023-01-09
npm ERR! notarget No matching version found for xxx@^1.0.64. npm 插件安装失败解决 2023-03-13