Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is hoped that the emit event can still be received after the component is destroyed #8450

Closed
zouyaoji opened this issue May 29, 2023 · 5 comments
Labels
✨ feature request New feature or request

Comments

@zouyaoji
Copy link

What problem does this feature solve?

我看3.2.33版本更新日志有一条:
I see that there is an entry in the update log of version 3.2.33:

runtime-core: ensure custom events are not emitted anymore after unmount. (#5679) (71c9536), closes #5674

我希望能增加一个配置项,能让组件销毁后外面也能够正常接收到emit的事件。
I hope to add a configuration item, so that the outside can normally receive the emit event after the component is destroyed.

因为有些场景可能还是需要这样,比如我基于了vue组件的生命周期写了一个地图组件,在组件创建时初始化了地图实例viewer,在组件销毁的时候去销毁了这个地图实例viewer。等viewe销毁后再emit的事件,外部都接收不了。

Because some scenarios may still need this, for example, I wrote a map component based on the life cycle of the Vue component, initialized the map instance viewer when the component was created, and destroyed the map instance viewer when the component was destroyed. The event that emits after the viewe is destroyed cannot be received by the outside world.

What does the proposed API look like?

non

@zouyaoji zouyaoji added the ✨ feature request New feature or request label May 29, 2023
@Alfred-Skyblue
Copy link
Member

Normally, after the instance of the component is destroyed, it should be garbage collected, so the emit event cannot be monitored, but if you manually destroy the map instance, you can choose not to destroy it, and you can use EventBus to trigger, it It is unreasonable behavior to listen to events and let destroyed components trigger events.

@zouyaoji
Copy link
Author

Ok, then I'll consider using mitt to do it myself.

@Killea
Copy link

Killea commented May 29, 2023

You could try to find the $parent and then call the method in the parent. You also could use provide/inject to pass the value if it's a deeply nested component.

@LinusBorg
Copy link
Member

Using a custom event emitter would be a better way to go. Vue won't support that

@zouyaoji
Copy link
Author

It is possible to use custom event, but it is really troublesome and cannot be bound to vue components. But I found a method that may not be conventional, which can continue to trigger events when the vue component is destroyed.

const instance = getCurrentInstance()
instance.isUnmounted = false

@github-actions github-actions bot locked and limited conversation to collaborators Sep 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
✨ feature request New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants