如何在rtk查询中配置企业中间件

发布于 2025-02-03 02:03:04 字数 1823 浏览 2 评论 0原文

redux-toolkit给定原始文档 -

import { configureStore } from '@reduxjs/toolkit'
// Or from '@reduxjs/toolkit/query/react'
import { setupListeners } from '@reduxjs/toolkit/query'
import { pokemonApi } from './services/pokemon'

export const store = configureStore({
  reducer: {
    // Add the generated reducer as a specific top-level slice
    [pokemonApi.reducerPath]: pokemonApi.reducer,
  },
  // Adding the api middleware enables caching, invalidation, polling,
  // and other useful features of `rtk-query`.
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware().concat(pokemonApi.middleware),
})

// optional, but required for refetchOnFocus/refetchOnReconnect behaviors
// see `setupListeners` docs - takes an optional callback as the 2nd arg for customization
setupListeners(store.dispatch)

我的代码

import { configureStore } from '@reduxjs/toolkit'
import newletterReducer from '../slicers/newletter/newsletterSlicer'
import { setupListeners } from '@reduxjs/toolkit/query'
import { getmoviesdetails } from '../slicers/service/getmoviedetails'
import { authService } from '../slicers/service/auth/authService'

export const store = configureStore({
    reducer: {
        newsletter: newletterReducer,
        [getmoviesdetails.reducerPath]: getmoviesdetails.reducer,
        [authService.reducerPath]: authService.reducer,
    },
    middleware:
        (getdefaultMiddleware) =>
            {return getdefaultMiddleware().concat(getmoviesdetails.middleware)
                ,getdefaultMiddleware().concat(authService.middleware)}
                     
            

})

setupListeners(store.dispatch)

他们给出了错误 buildInitiate.ts:248警告:中间件 rtk-Query API在Reducerpath“ GetMoviedEtails”中尚未添加到商店中。 诸如自动缓存收集,自动换炉等诸如无法使用之类的功能。

redux-toolkit given original doc--

import { configureStore } from '@reduxjs/toolkit'
// Or from '@reduxjs/toolkit/query/react'
import { setupListeners } from '@reduxjs/toolkit/query'
import { pokemonApi } from './services/pokemon'

export const store = configureStore({
  reducer: {
    // Add the generated reducer as a specific top-level slice
    [pokemonApi.reducerPath]: pokemonApi.reducer,
  },
  // Adding the api middleware enables caching, invalidation, polling,
  // and other useful features of `rtk-query`.
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware().concat(pokemonApi.middleware),
})

// optional, but required for refetchOnFocus/refetchOnReconnect behaviors
// see `setupListeners` docs - takes an optional callback as the 2nd arg for customization
setupListeners(store.dispatch)

my code

import { configureStore } from '@reduxjs/toolkit'
import newletterReducer from '../slicers/newletter/newsletterSlicer'
import { setupListeners } from '@reduxjs/toolkit/query'
import { getmoviesdetails } from '../slicers/service/getmoviedetails'
import { authService } from '../slicers/service/auth/authService'

export const store = configureStore({
    reducer: {
        newsletter: newletterReducer,
        [getmoviesdetails.reducerPath]: getmoviesdetails.reducer,
        [authService.reducerPath]: authService.reducer,
    },
    middleware:
        (getdefaultMiddleware) =>
            {return getdefaultMiddleware().concat(getmoviesdetails.middleware)
                ,getdefaultMiddleware().concat(authService.middleware)}
                     
            

})

setupListeners(store.dispatch)

they given error
buildInitiate.ts:248 Warning: Middleware for RTK-Query API at reducerPath "getmoviedetails" has not been added to the store.
Features like automatic cache collection, automatic refetching etc. will not be available.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2025-02-10 02:03:04

您可以这样做:

middleware:
        (getdefaultMiddleware) =>
            getdefaultMiddleware()
            .concat([
                     getmoviesdetails.middleware, 
                     authService.middleware
                   ])

基本上,您可以将所有中间件传递在数组中。

You can do it like this:

middleware:
        (getdefaultMiddleware) =>
            getdefaultMiddleware()
            .concat([
                     getmoviesdetails.middleware, 
                     authService.middleware
                   ])

Basically you can pass all your middleware in a array.

人心善变 2025-02-10 02:03:04
getdefaultMiddleware().concat(getmoviesdetails.middleware).concat(authService.middleware)

但是,除非这些API是完全独立的API,否则您实际上应该只有一个API。

引用文档:

通常,您的应用程序需要与之通信的每个基本URL只有一个API切片。例如,如果您的网站从 /api /post和 /api /用户中获取数据,则您将拥有单个API Slice,其中包含 /API /作为基本URL,并且为帖子和用户提供了单独的端点定义。这使您可以通过跨端点定义标签关系有效地利用自动重新提取。

出于可维护性,您可能希望将端点定义跨多个文件拆分,同时仍维护一个包含所有这些端点的单个API片。有关如何使用InjectEndPoints属性将API端点从其他文件注入单个API Slice定义。

getdefaultMiddleware().concat(getmoviesdetails.middleware).concat(authService.middleware)

But you really should only have one api unless those are completely independent apis that will never have any overlapping data ever.

Quoting from the docs:

Typically, you should only have one API slice per base URL that your application needs to communicate with. For example, if your site fetches data from both /api/posts and /api/users, you would have a single API slice with /api/ as the base URL, and separate endpoint definitions for posts and users. This allows you to effectively take advantage of automated re-fetching by defining tag relationships across endpoints.

For maintainability purposes, you may wish to split up endpoint definitions across multiple files, while still maintaining a single API slice which includes all of these endpoints. See code splitting for how you can use the injectEndpoints property to inject API endpoints from other files into a single API slice definition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文