问题
Is there a way to recreate a component in Svelte?
The context is that I have a component which has already been created. Once it completes its process, I would like for it to be destroyed and then created again. This would restore its properties to their default state, and ensure that the same is done for its nested components.
I imagine that this is technically possible with the existing component methods, but I was wondering if there's a straightforward way to go about it.
As an aside, I'm aware that recreating a component might be inefficient in some contexts. It might be preferable to achieve the desired recreation effect by manually resetting the state of the component and its nested components. For instance, a recursive function could somehow traverse the component and its nested components, and invoke the default data function of each. However, this is not so straightforward, and there are problematic aspects (i.e. computed properties and creation lifecycle hooks).
回答1:
The short answer is no, to recreate a component in its factory-fresh state you would need to call new Thing(...)
again.
来源:https://stackoverflow.com/questions/48911519/recreate-a-component