as title, how can I do that
from offical documentation just tell us that $delete can use argument 'object' and 'key'
but I want delete a component by itself like this
this.$delete(this)
To make a component delete itself in Vue. js, we can use the this. $destroy method. to add the close method to call this.
Since components are reusable Vue instances, they accept the same options as new Vue , such as data , computed , watch , methods , and lifecycle hooks. The only exceptions are a few root-specific options like el .
To remount a component in Vue. js, we should change the key prop of the component. to set the key prop of my-component to componentKey . And then we add a button to toggle the componentKey between true and false .
I couldn't find instructions on completely removing a Vue instance, so here's what I wound up with:
module.exports = { ... methods: { close () { // destroy the vue listeners, etc this.$destroy(); // remove the element from the DOM this.$el.parentNode.removeChild(this.$el); } } };
Vue 3 is basically the same, but you'd use root
from the context argument:
export default { setup(props, { root }){ const close = () => { root.$destroy(); root.$el.parentNode.removeChild(root.$el); }; return { close }; } }
In both Vue 2 and Vue 3 you can use the instance you created:
const instance = new Vue({ ... }); ... instance.$destroy(); instance.$el.parentNode.removeChild(instance.$el);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With