Component lifecycle listener in Vue3 #12342
Unanswered
ReinisV
asked this question in
Help/Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In Vue2 it was possible possible to dynamically add listeners for component lifecycle events through the events api, e.g.
that is no longer possible in Vue3 due to the removal of
$once
,$on
, etc.The question is simple -
given I have a Vue component instance reference, how do I subscribe to its lifecycle events? For simplicities sake, lets say I am using options api.
this feature is useful for example for writing custom meta libraries, that would listen for mounted events to do DOM work or for beforeUnmount events to do clean up work when the component gets destroyed (and so the associated work should also be disposed).
heres a stackoverflow question about this very same thing:
https://stackoverflow.com/questions/48931936/how-to-add-vue-lifecycle-listener-dynamically
the answers for Vue2 are valid, but none of the Vue3 answers work (because they mostly suggest updating component definitions hook array, which can be too late, if the component instance is already initialized and has its hooks set, and also can cause unnecessary duplication, because what the user wants is to get notified of the current instances beforeDestroy event, not all instances beforeDestroy events).
Beta Was this translation helpful? Give feedback.
All reactions