我试图在装载机props 中使用参数props ,因为我使用的是Typescript ,我定义的参数如下所示,但Typescript 对我大喊大叫我不知道为什么? 请以任何我能避开的方式将此告知,提前谢谢.

Type '({ params }: CustomLoaderFunctionArgs) => Promise<any>' is not assignable to type 'LoaderFunction<any>'.
  Type '({ params }: CustomLoaderFunctionArgs) => Promise<any>' is not assignable to type '(args: LoaderFunctionArgs<any>) => DataFunctionValue | Promise<DataFunctionValue>'.
    Types of parameters '__0' and 'args' are incompatible.
      Type 'LoaderFunctionArgs<any>' is not assignable to type 'CustomLoaderFunctionArgs'.
        Type 'LoaderFunctionArgs<any>' is not assignable to type '{ params: { id: string; }; }'.
          Types of property 'params' are incompatible.
            Property 'id' is missing in type 'Params<string>' but required in type '{ id: string; }'.ts(2322)
patient-form.tsx(45, 66): 'id' is declared here.
components.d.ts(61, 5): The expected type comes from property 'loader' which is declared here on type 'IntrinsicAttributes & RouteProps'
(property) loader: ({ params }: CustomLoaderFunctionArgs) => Promise<any>
No quick fixes available
function App() {
  const router = createBrowserRouter(
    createRoutesFromElements(
      <Route path="/" element={<RootLayout />}>
        <Route index element={<IndexPage />} />
        <Route path="about" element={<ContactPage />} />
        <Route path="sign-in" element={<SignInPage />} />
        <Route path="sign-up" element={<SignUpPage />} />
        <Route path="dashboard" element={<DashboardLayout />}>
          <Route index element={<DashboardPage />} loader={patientsDataLoader} />
          <Route path="patient/:id" element={<PatientForm initialData={null} />} loader={patientDataLoader}/>
        </Route>
      </Route>
    )
  );

  return <RouterProvider router={router} />;
}

export default App;
export const patientDataLoader = async ({params} : {params: {id: string}}) => {
  const response = await axios.get(`http://localhost:3000/api/patient/${params.id}`);
  return response.data;
}

推荐答案

问题在于您将参数传递给函数的方式.在文档中,提到了如何将参数传递给装载器函数.请判断并判断以下解决方案.

createBrowserRouter([
  {
    path: "/teams/:teamId",
    loader: ({ params }) => {
      return fakeGetTeam(params.teamId);
    },
  },
]);

对于您的功能:

export const patientDataLoader = async ({ params }) => {
  const response = await axios.get(`http://localhost:3000/api/patient/${params.id}`);
  return response.data;
}

文档链接:https://reactrouter.com/en/main/route/loader

Typescript相关问答推荐

如何在Jest中嘲笑location.back()方法调用进行Angular 组件测试?

当类型断言函数返回假时,TypScript正在推断从不类型

即使子网是公共的,AWS CDK EventBridge ECS任务也无法分配公共IP地址

为什么typescript不能推断类的方法返回类型为泛型''

React重定向参数

Vue SFC中的多个脚本块共享导入的符号

如何将泛型对象作为返回类型添加到类型脚本中

带下拉菜单的Angular 响应式选项卡

布局组件中的Angular 17命名路由出口

TypeScrip:强制使用动态密钥

如何将对象数组中的键映射到另一种对象类型的键?

如何从对象或类动态生成类型

是否可以通过映射类型将函数参数约束为预定义类型?

Angular NG8001错误.组件只能在一个页面上工作,而不是在她的页面上工作

是否可以在类型脚本中定义条件返回类型,而不在函数体中使用类型转换?

Typescript泛型调用对象';s方法

TypeScript中的这些条件类型映射方法之间有什么区别?

为什么过滤器不改变可能的类型?

带有过滤键的 Typescript 映射类型

是否有可能不允许在 TypeScript 中设置类属性,但也会抛出运行时错误?