英文:
Vue3 v-for using pinia computed property not updating correctly
问题
以下是您要翻译的内容:
组件
<template>
<div class="employees">
<EmployeeCard
v-for="(employee, idx) in filteredEmployeeList"
:key="idx"
:employee-data="employee"
/>
</div>
</template>
<script setup>
import { storeToRefs } from 'pinia';
import EmployeeFilter from '@/components/EmployeeFilter.vue';
import EmployeeCard from '@/components/EmployeeCard.vue';
import { useEmployeesStore } from '@/stores/employees';
const employeesStore = useEmployeesStore();
const { filteredEmployeeList } = storeToRefs(employeesStore);
</script>
筛选组件
<div class="filter-wrapper">
<div
v-for="(office, idx) in employeesStore.offices"
class="office-filter"
>
<label :for="idx">
<input
v-model="employeesStore.selectedOffices"
:value="office"
class="checkbox"
type="checkbox"
>
{{ office }}
</label>
</div>
</div>
存储
const selectedOffices = ref([]);
const selectedFiltersLength = computed(() => {
return selectedOffices.value.length;
});
const filteredEmployeeList = computed(() => {
// No filters selected, return all employees
if (selectedFiltersLength.value == 0) {
return employees.value;
}
return employees.value.filter(employee => selectedOffices.value.includes(employee.office));
});
英文:
I have a component that fetches employees from a pinia store, and a filter component to filter employees by office.
When I inspect the data in Vue devtools, both selectedOffices
and filteredEmployeeList
looks as expected. But the list rendered in the component (filteredEmployeeList) doesn't match the data displayed in devtools.
Any ideas?
Component
<template>
<div class="employees">
<EmployeeCard
v-for="(employee, idx) in filteredEmployeeList"
:key="idx"
:employee-data="employee"
/>
</div>
</template>
<script setup>
import { storeToRefs } from 'pinia';
import EmployeeFilter from '@/components/EmployeeFilter.vue';
import EmployeeCard from '@/components/EmployeeCard.vue';
import { useEmployeesStore } from '@/stores/employees';
const employeesStore = useEmployeesStore();
const { filteredEmployeeList } = storeToRefs(employeesStore);
</script>
Filter component
<div class="filter-wrapper">
<div
v-for="(office, idx) in employeesStore.offices"
class="office-filter"
>
<label :for="idx">
<input
v-model="employeesStore.selectedOffices"
:value="office"
class="checkbox"
type="checkbox"
>
{{ office }}
</label>
</div>
</div>
Store
const selectedOffices = ref([]);
const selectedFiltersLength = computed(() => {
return selectedOffices.value.length;
});
const filteredEmployeeList = computed(() => {
// No filters selected, return all employees
if (selectedFiltersLength.value == 0) {
return employees.value;
}
return employees.value.filter(employee => selectedOffices.value.includes(employee.office));
});
答案1
得分: 0
谢谢,找到了问题。正如你所说,问题出在:key属性上。将其更改为employee.id,现在它像魔法般正常运行。
英文:
Thanks, found the problem. It was as you said, problem with the :key attribute. Changed to employee.id and it works like a charm.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论