代码之家  ›  专栏  ›  技术社区  ›  jukenduit

使用RTK查询和vite的代理问题

  •  0
  • jukenduit  · 技术社区  · 2 年前

    我开始使用 Vite 而不是 CRA 但我无法让代理工作。

    我正在使用 json server 对于我的数据库,它位于 http://localhost:3000/customers

    对于CRA,我想补充一点 "proxy": "http://localhost:3000", 到 package.json 然后在我的apiSlice中:

    const baseQuery = fetchBaseQuery({ baseUrl: "" });
    

    然而,这对Vite不起作用,所以我尝试将其添加到 vite.config :

    server: {
            proxy: {
                "/api": "http://localhost:3000",
            },
        },
    

    并改变了我的 baseQuery 到

    const baseQuery = fetchBaseQuery({ baseUrl: "/api" });
    

    然而,我在控制台中不断收到此错误:

    CustomerList.jsx:4 Uncaught TypeError: useGetCustomersQuery is not a function or its return value is not iterable
    

    useGetCustomersQuery 是我的RTK查询,我认为问题是没有与通信 backend 正如它应该做的那样。

    1 回复  |  直到 2 年前
        1
  •  0
  •   phry    2 年前

    不,你的问题是 useGetCustomersQuery 未设置。这与数据获取无关。

    你可能进口了 createApi 从…起 @reduxjs/toolkit/query 而不是来自 @reduxjs/toolkit/query/react .