You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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