[英]How to update parent data after filter props in child component?
I want to change my parent component data after filtering month in the child component. 我想在子组件中过滤月份之后更改我的父组件数据。 The problem is where I want to download excel data using vue-json-excel the filter month in the parent component don't want to update.
问题是我想使用vue-json-excel下载excel数据,而父组件中的过滤器月份不想更新。
I have tried to using onChange and emitting event with data in parent, after that update the data in parent. 我尝试使用onChange并在父级中的数据上发出事件,之后更新父级中的数据。 I have tried changing data using computed, but it won't work.
我曾尝试使用计算更改数据,但无法正常工作。
## Parent (App.vue) ##
<template>
</div>
<TodosList
v-bind:todos="todos"
:editedTodo="editedTodo"
:selectedMonth="selectedMonth"
:selectedYear="selectedYear"
:months="months"
:years="years"
:json_data="json_data"
:STORAGE_KEY="STORAGE_KEY"
@change="changeSelectedMonth"
/>
<download-excel
class="button"
:fields="json_fields"
:fetch="fetchData"
worksheet="My Worksheet"
name="capaian_kinerja_pegawai.xls"
>Download Excel</download-excel>
</div>
</template>
<script>
methods: {
changeSelectedMonth(val) {
this.selectedMonth = val;
},
async fetchData() {
console.log(this.selectedMonth);
let selectedMonth = this.selectedMonth;
let data = this.todos;
let ret = data.filter(function(data) {
return data.month === selectedMonth;
});
return ret;
}
},
</script>
## Child (TodosList.vue) ##
<template>
<div>
<select v-model="selectedMonth" style="width:30%;" @change="onChange(selectedMonth)">
<option v-for="month in months" :key="month" :selected="selectedMonth === month">{{ month }}</option>
</select>
</div>
</template>
<script>
methods: {
onChange(newChangedMonth) {
this.$emit("changed", newChangedMonth);
}
}
};
</script>
I expect this.selectedMonth in async fetchData() changed to chosen option, so before I download the excel, I can filter based on selectedMonth. 我希望异步fetchData()中的this.selectedMonth更改为selected选项,因此在下载Excel之前,我可以基于selectedMonth进行过滤。 It always returns the default selectedMonth value.
它总是返回默认的selectedMonth值。
You are emitting "changed"
event in the child, while listening to "change"
event on the parent. 您正在孩子中发出
"changed"
事件,同时在父级上听到"change"
事件。
Use this: 用这个:
this.$emit("change", newChangedMonth);
instead of this: 代替这个:
this.$emit("changed", newChangedMonth);
Listen for changed event not change 监听变化的事件不变
<TodosList
v-bind:todos="todos"
:editedTodo="editedTodo"
:selectedMonth="selectedMonth"
:selectedYear="selectedYear"
:months="months"
:years="years"
:json_data="json_data"
:STORAGE_KEY="STORAGE_KEY"
@changed="changeSelectedMonth"
/>
OR emit change event 或发出变更事件
this.$emit("change", newChangedMonth);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.