Vue3中使用Pinia的计算属性(v-for)未正确更新。

huangapple go评论58阅读模式
英文:

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

&lt;template&gt;
&lt;div class=&quot;employees&quot;&gt;
  &lt;EmployeeCard
    v-for=&quot;(employee, idx) in filteredEmployeeList&quot;
    :key=&quot;idx&quot;
    :employee-data=&quot;employee&quot;
  /&gt;
&lt;/div&gt;
&lt;/template&gt;

&lt;script setup&gt;
import { storeToRefs } from &#39;pinia&#39;;
import EmployeeFilter from &#39;@/components/EmployeeFilter.vue&#39;;
import EmployeeCard from &#39;@/components/EmployeeCard.vue&#39;;

import { useEmployeesStore } from &#39;@/stores/employees&#39;;
const employeesStore = useEmployeesStore();
const { filteredEmployeeList } = storeToRefs(employeesStore);

&lt;/script&gt;

Filter component

&lt;div class=&quot;filter-wrapper&quot;&gt;
      &lt;div 
        v-for=&quot;(office, idx) in employeesStore.offices&quot; 
        class=&quot;office-filter&quot;
      &gt;
        &lt;label :for=&quot;idx&quot;&gt;
          &lt;input 
            v-model=&quot;employeesStore.selectedOffices&quot;
            :value=&quot;office&quot;
            class=&quot;checkbox&quot;
            type=&quot;checkbox&quot;
          &gt;
            {{ office }}
        &lt;/label&gt;
      &lt;/div&gt;
&lt;/div&gt;

Store

const selectedOffices = ref([]);

const selectedFiltersLength = computed(() =&gt; {
    return selectedOffices.value.length;
  });

const filteredEmployeeList = computed(() =&gt; {
   // No filters selected, return all employees 
   if (selectedFiltersLength.value == 0) {
      return employees.value;
    }

    return employees.value.filter(employee =&gt; selectedOffices.value.includes(employee.office));
  });

Screenshot

答案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.

huangapple
  • 本文由 发表于 2023年4月17日 15:59:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定