组件如何在 Vue 2.0 中删除自身 [英] how can component delete itself in Vue 2.0

查看:56
本文介绍了组件如何在 Vue 2.0 中删除自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为标题,我该怎么做

来自官方文档只是告诉我们 $delete 可以使用参数 'object' 和 'key'

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)

推荐答案

不,您将无法直接删除组件.父组件必须使用 v-if 从 DOM 中移除子组件.

No, you will not be able to delete a component directly. The parent component will have to use v-if to remove the child component from the DOM.

参考:https://vuejs.org/v2/api/#v-if

引自文档:

根据表达式值的真实性有条件地渲染元素.元素及其包含的指令/组件在切换期间被破坏和重新构造.

Conditionally render the element based on the truthy-ness of the expression value. The element and its contained directives / components are destroyed and re-constructed during toggles.

如果子组件是作为父级某个数据对象的一部分创建的,则必须通过 $emit 向父级发送事件,修改(或删除)数据,子组件将自行离开.最近还有一个问题:删除一个Vue子组件

If the child component is created as part of some data object on parent, you will have to send an event to parent via $emit, modify (or remove) the data and the child component will go away on its own. There was another question on this recently: Delete a Vue child component

这篇关于组件如何在 Vue 2.0 中删除自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆