简体   繁体   English

Vue.js - 从父组件中清除子表单字段

[英]Vue.js - Clearing Child Form Fields from Parent Component

I have a child sidebar component with some dropdowns.我有一个带有一些下拉菜单的子侧边栏组件。 Simplified example:简化示例:

在此处输入图像描述

You select a status here and it re renders the table/dashboard view in the parent component.您 select 在这里的状态,它重新呈现父组件中的表格/仪表板视图。 That is handled via $emit in the child component like so:这是通过子组件中的$emit处理的,如下所示:

selectedStatus: function(status) {
  this.$emit('updateStatus', status)
},

No issues there.那里没有问题。 Now I'd like to be able to clear or reset this input from the parent component.现在我希望能够从父组件中清除重置此输入。 As I said this is a simplified example and there are many dropdowns, a couple datetime selectors, and other inputs to clear.正如我所说,这是一个简化的示例,有许多下拉菜单、几个日期时间选择器和其他要清除的输入。

If I do something like如果我做类似的事情

this.selectedStatus = [] in the parent, it does indeed reset/re render the table/data properly but it still shows the UI piece (ie, the selected status in the dropdown). this.selectedStatus = []在父级中,它确实会正确重置/重新渲染表/数据,但它仍然显示 UI 部分(即下拉列表中的选定状态)。

If I do something like如果我做类似的事情

this.selectedStatus = [] in the child it does correctly remove/alter the UI piece to where it clears the dropdown. this.selectedStatus = []在孩子中它确实正确地删除/更改了 UI 部分到它清除下拉列表的位置。

What is the correct approach here to clear child filters/inputs from the parent?从父级清除子级过滤器/输入的正确方法是什么?

I recommend you to read this article , it explains all the ways to make a child component rerender (and therefore reset it's state).我建议你阅读这篇文章,它解释了使子组件重新渲染(并因此重置它的状态)的所有方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM