vue 组件中的钩子函数 不能直接写this

vue

export default {

data(){

return {

num: 18

}

},

beforeRouteEnter(to, from, next){

next(vm=>{

vm.num=19;

})

}

}

  • vm 表示this ,可以调用 num了

三个钩子函数: beforeRouteEnter, beforeRouteUpdata, beforeRouteLeave

const Foo = {

template: `...`,

beforeRouteEnter (to, from, next) {

// 在渲染该组件的对应路由被 confirm 前调用

// 不!能!获取组件实例 `this`

// 因为当钩子执行前,组件实例还没被创建
   next() // 必须有这个,相当于一个按钮开启一样。

},

beforeRouteUpdate (to, from, next) {

// 在当前路由改变,但是该组件被复用时调用

// 举例来说,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候,

// 由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。

// 可以访问组件实例 `this`

   next() // 必须有这个,相当于一个按钮开启一样

  },

beforeRouteLeave (to, from, next) {

// 导航离开该组件的对应路由时调用

// 可以访问组件实例 `this`

  next() // 必须有这个,相当于一个按钮开启一样

  }

}

以上是 vue 组件中的钩子函数 不能直接写this 的全部内容, 来源链接: utcz.com/z/380169.html

回到顶部