hello , I have a question, How to listener lifecycle hook in vue3 like vue2? vue 2
mounted() { const onResize = () => { console.log('onResize') } window.addEventListener('resize', onResize) // hook:liefHook this.$once('hook:beforeDestroy', () => { window.removeEventListener('resize', onResize) }) }
vue3 does not support this.$once anymore. I use npm to do this according to offical docs and lifecyle hook event.
this.$once
onMounted(() => { console.log('vue3 mounted') function onResize() { console.log('onResize') } window.addEventListener('resize', onResize) event.once('vnode-before-unmount', () => { console.log('vnode-before-unmount') removeEventListener('resize', onResize) }) })
event:
import emitter from 'tiny-emitter/instance' export const event = { emit: (...args) => emitter.emit(...args), on: (...args) => emitter.on(...args), off: (...args) => emitter.off(...args), once: (...args) => emitter.once(...args), }
but event.once does not call when component umount. Could u give me some advices? thinks
event.once
Try onUnmount hook
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
hello , I have a question, How to listener lifecycle hook in vue3 like vue2?
vue 2
vue3 does not support
this.$once
anymore. I use npm to do this according to offical docs and lifecyle hook event.event:
but
event.once
does not call when component umount.Could u give me some advices? thinks
Try onUnmount hook