简体   繁体   English

Vue 3, Pinia - getter 没有过滤项目

[英]Vue 3, Pinia - getter is not filtering items

I have a following code for the template:我有以下模板代码:

<script setup>

import {useProductStore} from "@/store/products";
import {ref} from "vue";
import {storeToRefs} from "pinia";

const productStore = useProductStore()

let { products } = storeToRefs(productStore)
let { categories } = storeToRefs(productStore)

const { showProducts } = productStore

</script>

<template>

  <div>
    <div class="row">
      <div class="col-md-2">
        <button class="d-block w-100"
                @click="showProducts('all')">All</button>
        <button class="d-block w-100"
                @click="showProducts(category.id)"
                :key="category.id"
                v-for="category in categories">{{ category.name }}</button>
      </div>
      <div class="col">
        <p v-for="product in showProducts('all')"
            :key="product.id">{{ product.name }}</p>
      </div>
    </div>
  </div>

</template>

And the code for the Pinia store: Pinia 商店的代码:

import { defineStore } from 'pinia'
import axiosClient from '@/axios'

export const useProductStore = defineStore( 'products', {
    id : 'products',
    state: () => {
        return {
            products: [],
            categories: [],
        }
    },
    actions: {
        async getProducts()
        {

            axiosClient.get('products')
                .then((response) => {
                    const data = response.data

                    this.products = data.products
                    this.categories = data.categories
                })

        }
    },
    getters: {
        showProducts: (state) =>
        {
            return (categoryID) =>
            {
                if(categoryID === 'all')
                {
                    return state.products
                }

                return state.products.filter((product) => product.productCategoryID == categoryID)
            }
        }
    },
})

The initial data is loaded and show,but on click event is not showing filtered data (the showProducts method is fired and products are filtered, but the list is not visually changed).初始数据已加载并显示,但单击事件未显示过滤数据(触发 showProducts 方法并过滤产品,但列表未在视觉上更改)。

What am I doing wrong here?我在这里做错了什么? What什么

This is presumably due to the behaviour of the getter when used with arguments.这可能是由于 getter 与参数一起使用时的行为。 The Documentation states, that using them the way you do, that the getter is no longer a getter, but a function that is simply invoked, which would suggest, that you have to invoke it after changing them again. 文档指出,按照您的方式使用它们,getter 不再是 getter,而是一个简单调用的函数,这表明您必须在再次更改它们后调用它。

I'd suggest to filter the results in the component itself and map the given states.我建议过滤组件本身的结果并映射给定的状态。

Something along these lines:这些方面的东西:

DISCLAIMER I didn't check if the codes runs as it is written below, the code is shown to give a general idea of what I suggest.免责声明我没有检查代码是否按照下面的说明运行,显示的代码是为了大致了解我的建议。

Template :模板

<script setup>
import {useProductStore} from "@/store/products";
import {ref, reactive, computed} from "vue";
import {storeToRefs} from "pinia";

const productStore = useProductStore()

const state = reactive({categoryId: 'all'});

const filteredProducts = computed(() => {
   return state.categoryId === 'all' 
     ? products
     : products.filter((product) => product.productCategoryID === categoryId);
})

let { products } = storeToRefs(productStore)
let { categories } = storeToRefs(productStore)
</script>

<template>

  <div>
    <div class="row">
      <div class="col-md-2">
        <button class="d-block w-100"
                @click="categoryId = 'all'">All</button>
        <button class="d-block w-100"
                @click="categoryId = category.id"
                :key="category.id"
                v-for="category in categories">{{ category.name }}</button>
      </div>
      <div class="col">
        <p v-for="product in filteredProducts"
            :key="product.id">{{ product.name }}</p>
      </div>
    </div>
  </div>
</template>

Pinia :松树

import { defineStore } from 'pinia'
import axiosClient from '@/axios'

export const useProductStore = defineStore( 'products', {
    id : 'products',
    state: () => {
        return {
            products: [],
            categories: [],
        }
    },
    actions: {
        async getProducts()
        {
          axiosClient.get('products')
            .then((response) => {
              const data = response.data
              this.products = data.products
              this.categories = data.categories
            })
        }
    },
})

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

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