Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can component delete itself in Vue 2.0

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) 
like image 366
Ciao Avatar asked Nov 06 '16 01:11

Ciao


People also ask

How do I delete a component on Vue?

To make a component delete itself in Vue. js, we can use the this. $destroy method. to add the close method to call this.

Does Vue reuse components?

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 .

How do you remount a component in Vue?

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 .


1 Answers

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); 
like image 173
bendytree Avatar answered Sep 28 '22 21:09

bendytree