简体   繁体   English

编译失败。/src/Content.js 尝试导入错误:“重定向”未从“react-router-dom”导出

[英]Failed to compile ./src/Content.js Attempted import error: 'Redirect' is not exported from 'react-router-dom'

File package.json文件package.json

{
  "name": "acc",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^13.5.0",
    "axios": "0.27.2",
    "devextreme": "22.1.4",
    "devextreme-react": "^22.1.4",
    "exceljs": "4.3.0",
    "file-saver": "^2.0.5",
    "react": "18.2.0",
    "react-app-polyfill": "3.0.0",
    "react-axios": "^2.0.6",
    "react-dom": "18.2.0",
    "react-router-dom": "6.3.0",
    "react-scripts": "^2.1.3",
    "sass": "^1.34.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "build-themes": "devextreme build",
    "postinstall": "npm run build-themes"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "devextreme-cli": "1.3.2",
    "devextreme-themebuilder": "^21.2.4"
  }
}

File Content.js文件Content.js

import React from 'react';
import {Switch, Route, Redirect} from 'react-router-dom';
import appInfo from './app-info';
import routes from './app-routes';
import {SideNavOuterToolbar as SideNavBarLayout} from './layouts';
import {Footer} from './components';

export default function Content() {
    return (
        <SideNavBarLayout title={appInfo.title}>
            <Switch>
                {routes.map(({path, component}) => (
                    <Route
                        exact
                        key={path}
                        path={path}
                        component={component}
                    />
                ))}
                <Redirect to={'/home'}/>
            </Switch>
            <Footer>
                Copyright © 2011-{new Date().getFullYear()} {appInfo.title} Inc.
                <br/>
                All trademarks or registered trademarks are property of their
                respective owners.
            </Footer>
        </SideNavBarLayout>
    );
}

在此处输入图像描述 Error错误

Failed to compile
./src/Content.js
Attempted import error: 'Redirect' is not exported from 'react-router-dom'.

在此处输入图像描述 How to fix this error?如何修复此错误?

Issues问题

react-router-dom@6 doesn't export any Redirect component, along with also not exporting any Switch component. react-router-dom@6不会导出任何Redirect组件,也不会导出任何Switch组件。 I'm surprised there's no error for that ( perhaps there is and you're only asking about the last error ).我很惊讶没有错误(也许有,你只是在问最后一个错误)。

Additionally the Route component API/syntax changed significantly;此外, Route组件 API/语法发生了显着变化; it no longer takes component , or render or children function props and instead now takes a single element prop taking a ReactNode , aka JSX, as a prop value.它不再需要component ,或renderchildren function props,而是现在需要一个单element prop,将ReactNode (又名 JSX)作为 prop 值。

Solution解决方案

Replace the Switch component with the Routes component and replace the Redirect component with a route rendering the Navigate component.Switch组件替换为Routes组件,并将Redirect组件替换为呈现Navigate组件的路由。

Map the routes array to the RRDv6 Route component where component is passed to the element prop. Map routes数组到 RRDv6 Route组件,其中component被传递给element prop。

Here's an example conversion:这是一个示例转换:

import { Routes, Route, Navigate } from 'react-router-dom';
...

export default function Content() {
  return (
    <SideNavBarLayout title={appInfo.title}>
      <Routes> // <-- Routes replaces Switch
          {routes.map(({ path, component: Component }) => (
            <Route
              key={path}
              path={path}
              element={<Component />} // <-- component passed as JSX
            />
          ))}
          <Route // <-- RRDv6 Redirect equivalent
            path="*"
            element={<Navigate to="/home" replace />}
          />
        </Route>
      </Routes>
      <Footer>
        Copyright © 2011-{new Date().getFullYear()} {appInfo.title} Inc.
        <br/>
        All trademarks or registered trademarks are property of their respective owners.
      </Footer>
    </SideNavBarLayout>
  );
}

FYI, if you are rendering a SideNavBarLayout "layout" then you might want to invest time in learning about Layout Routes and convert SideNavBarLayout into a layout route by rendering an Outlet instead of the children prop.仅供参考,如果您正在渲染SideNavBarLayout “布局”,那么您可能需要花时间学习布局路由,并通过渲染Outlet而不是children道具将SideNavBarLayout转换为布局路由。

Version 6 of React Router doesn't export Redirect so you simply have to use either navigate() or declaratively as <Navigate... /> React Router 版本 6 不导出Redirect ,因此您只需使用navigate()或以声明方式作为<Navigate... />

Source: https://reactrouter.com/docs/en/v6/upgrading/v5#remove-redirects-inside-switch来源: https://reactrouter.com/docs/en/v6/upgrading/v5#remove-redirects-inside-switch

Please also see: How to redirect in React Router v6?另请参阅: 如何在 React Router v6 中重定向?

You have added router version 6 and your syntax seems like of react router version 5您已经添加了路由器版本 6,并且您的语法看起来像反应路由器版本 5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ./src/App.jsx 尝试导入错误:“路由”未从“react-router-dom”导出 - ./src/App.jsx Attempted import error: 'Routes' is not exported from 'react-router-dom' 尝试导入错误:“Navigate”未从“react-router-dom”导出 - Attempted import error: 'Navigate' is not exported from 'react-router-dom' 尝试导入错误:“Outlet”未从“react-router-dom”导出 - Attempted import error: 'Outlet' is not exported from 'react-router-dom 尝试导入错误:“PrivateRoute”未从“react-router-dom”导出 - Attempted import error: 'PrivateRoute' is not exported from 'react-router-dom' 尝试导入错误:“路由”未从“react-router-dom”导出 - Attempted import error: 'Routes' is not exported from 'react-router-dom' 尝试导入错误:“BrowseRouter”未从“react-router-dom”导出(导入为“Router”) - Attempted import error: 'BrowseRouter' is not exported from 'react-router-dom' (imported as 'Router') 如何修复错误:尝试导入错误:“Navlink”未从“react-router-dom”导出 - How to fix the error: Attempted import error: 'Navlink' is not exported from 'react-router-dom' 导入错误:“useHistory”未从“react-router-dom”导出 - import error: 'useHistory' is not exported from 'react-router-dom' 反应:&#39;重定向&#39;不是从&#39;react-router-dom&#39;导出的 - React: 'Redirect' is not exported from 'react-router-dom' 'useNavigate' 不是从 'react-router-dom' 导出的 - 'useNavigate' is not exported from 'react-router-dom'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM