reactjs 交换机路由器在react应用程序中不工作

qyyhg6bp  于 2023-04-20  发布在  React
关注(0)|答案(1)|浏览(137)

交换机路由器在react应用程序中不工作i installed npm install react-router-dom
但还是不行
Error in ./src/App.js 12:35-41 export 'Switch'(imported as 'Switch')was not found in 'react-router-dom'(possible exports:AbortedDeferredError,Await,BrowserRouter,Form,HashRouter,Link,MemoryRouter,NavLink,Navigate,NavigationType,Outlet,Route,Router,RouterProvider,Routes,ScrollRestoration,UNSAFE_DataRouterContext,UNSAFE_DataRouterStateContext,UNSAFE_LocationContext,UNSAFE_NavigationContext,UNSAFE_RouteContext,UNSAFE_useScrollRestoration,createBrowserRouter,createHashRouter,createMemoryRouter,createPath,createRoutesFromChildren,createRoutesFromElements,createSearchParams,defer,generatePath,isRouteErrorResponse,json,matchPath,matchRoutes,parsePath,redirect,renderMatches,resolvePath,unstable_HistoryRouter,unstable_useBlocker,unstable_usePrompt,useActionData,useAsyncError,useAsyncValue,useBeforeUnload,useFetcher,useFetchers,useFormAction,useHref,useInRouterContext,useLinkClickHandler,useLoaderData,useLocation,useMatch,useMatches,useNavigation,useNavigationType,useOutlet,useOutletContext,useParams,useResolvedPath、useRevalidator、useRouteError、useRouteLoaderData、useRoutes、useSearchParams、useSubmit)
我安装了npm install react-router-dom,但它仍然不工作

ee7vknir

ee7vknir1#

关于您安装的react-router-dom版本信息不多,但我假设您安装的是最新版本6.10.0。在这种情况下,请参考以下答案:https://stackoverflow.com/a/69843676/17465266
tl;dr是Switch被替换为Routes。看看这里的例子(来自https://reactrouter.com/en/main/components/routes):

import {
  Routes, // instead of "Switch"
  Route,
} from "react-router-dom";

// ...

<Routes>
  <Route path="/" element={<Dashboard />}>
    <Route
      path="messages"
      element={<DashboardMessages />}
    />
    <Route path="tasks" element={<DashboardTasks />} />
  </Route>
  <Route path="about" element={<AboutPage />} />
</Routes>

相关问题