如何发送数组作为 RTK 查询的参数?

How to send array as parameter for RTK Query?

我想像这样将数组作为参数发送:

v1/product?main_category[]=3&main_category[]=4 

我的参数:

params.main_category = [1,2,3]

我的产品切片:

export const productsSlice = apiSlice.injectEndpoints({
    endpoints: (builder) => ({
        getProducts: builder.query<
            any,
            {
                title: string;
                ename: string;
                status: string;
                site_product_id: number;
                site_product_meta_id: number;
                per_page: number;
                page: number;
                parent_id: [];
                main_category: [];
                stock_category: [];
                side_category: [];
            }
        >({
            query: (arg) => {
                const {
                    title: ename,
                    status,
                    site_product_id,
                    site_product_meta_id,
                    per_page,
                    page,
                    parent_id,
                    main_category,
                    stock_category,
                    side_category,
                } = arg;
                return {
                    url: "/product",
                    params: {
                        ename,
                        status,
                        site_product_id,
                        site_product_meta_id,
                        per_page,
                        page,
                        parent_id,
                        main_category,
                        stock_category,
                        side_category,
                    },
                };
            },
        }),
        getProduct: builder.query({
            query: (id) => `/product/${id}`,
        }),
    }),
});

但它是这样发送的:

/v1/product?main_category=3%2C4

默认情况下,RTK 查询仅使用 URLSearchParms 进行参数序列化,结果如您所见。但是您可以通过在 fetchBaseQuery 中指定 paramsSerializer 函数来覆盖此行为。这将允许您编写自己的序列化逻辑或使用像 query-string 这样的库来为您完成。