redux 具有两个不同API的RTK查询仅适用于一个API

ohfgkhjo  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(161)

我有两个不同的API。两个都使用RTK查询来做不同的CRUD操作集。其中一个工作得很好,但另一个显示错误

TypeError: (0 , _api_addressApi__WEBPACK_IMPORTED_MODULE_2__.useGetAddress) is not a 
function

这是我的addressApi.js,它不工作

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const addressApi = createApi({
  reducerPath: "address",
  baseQuery: fetchBaseQuery({
    baseUrl: "https://random-data-api.com/api/v2",
  }),
  endpoints: (builder) => ({
    getAddress: builder.query({
      query: () => {
        return { url: "/addresses" };
      },
    }),
  }),
});

export const { useGetAddress } = addressApi;

这是我的store.js

import { configureStore } from "@reduxjs/toolkit";

import { addressApi } from "../api/addressApi";
import { destinationApi } from "../api/destinationApi";

export const store = configureStore({
  reducer: {
    // destinationApi works but addressApi does not works
    [destinationApi.reducerPath]: destinationApi.reducer,
    [addressApi.reducerPath]: addressApi.reducer,
  },
  // Adding the api middleware enables caching, invalidation, polling,
  // and other useful features of `rtk-query`.
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware()
      .concat(destinationApi.middleware)
      .concat(addressApi.middleware),
});

在调用useGetAddress()的文件中

import { useGetAddress } from "../api/addressApi";
  function AddDestination() {
  ....
  const { data, error } = useGetAddress();
  console.log(data);
goucqfw6

goucqfw61#

提供方法:'GET'以查询
喜欢

getAddress: builder.query({
  query: () => {
    return { url: "/addresses" ,
             method:'GET',
           };
           },

相关问题