vuejs / Discussion

Vue.js discussion
167 stars 17 forks source link

How to add lifecycle hooks on the fly? #502

Closed gera2ld closed 8 years ago

gera2ld commented 8 years ago

I want to add a destroyed hook for a component instance, but this.destroyed seems not to work since it should be defined in the constructor. I could not find any related events to be listened in the document. I thought there would be something like this.$on('destroyed') but could not find any. So what is the right way to add lifecycle hooks on the fly?

LinusBorg commented 8 years ago

https://github.com/vuejs/vue/blob/dev/src/instance/internal/events.js#L178

this.$on('hook:destroyed') should work.

gera2ld commented 8 years ago

@LinusBorg Thanks, this is exactly what I was looking for. Is it safe to use it in the future since it's not documented?

LinusBorg commented 8 years ago

it works the same way in 2.0, and I'Äm not aware of any plans to change that behaviour anytime.