简体   繁体   English

如何修复错误:尝试导入错误:“Route”未从“react-router-dom”导出

[英]How to fix error: Attempted import error: 'Route' is not exported from 'react-router-dom'

-- Update: This started working when I just stopped the development server, quit VSCode and restarted it again. - 更新:当我刚刚停止开发服务器,退出 VSCode 并再次重新启动它时,这开始工作了。 Not sure what caused it.不知道是什么原因造成的。

Busy learning React and ran into this error.忙于学习 React,遇到了这个错误。 I have tried several other SO posts but can't seem to get an answer to my problem.我已经尝试了其他几个 SO 帖子,但似乎无法回答我的问题。

I'm following the guidance to use only react-router-dom and import BrowserRouter and Route from react-router-dom.我遵循仅使用 react-router-dom 并从 react-router-dom 导入 BrowserRouter 和 Route 的指导。 But I'm getting an error:但我收到一个错误:

./src/App.js
Attempted import error: 'Route' is not exported from 'react-router-dom'.

Not sure what I'm doing wrong here?不知道我在这里做错了什么?

Here is my App.js这是我的 App.js

import React, { Component } from 'react';
import Navbar from './components/Navbar'
import { BrowserRouter, Route } from 'react-router-dom'
import Home from './components/Home'

class App extends Component {
  render() {
    return (
      <BrowserRouter>
      <div className="App">
        <Navbar />
        <Route path='/' component={Home} />
      </div>
      </BrowserRouter>
    );
  }
}

export default App;

Here is my package.json:这是我的 package.json:

  "name": "poketimes",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.8.5",
    "react-dom": "^16.8.5",
    "react-router-dom": "^5.0.0",
    "react-scripts": "2.1.8"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

From what I've been reading, other posts still use the react-router separately, but if I'm understanding the documentation correctly, I'm not supposed to do that in this version?从我一直在阅读的内容来看,其他帖子仍然单独使用 react-router,但是如果我正确理解文档,我不应该在这个版本中这样做吗? Also, I'm following a tutorial that does this exactly and it seems to work.另外,我正在关注一个完全做到这一点的教程,它似乎有效。

Any advice would be greatly appreciated, thanks!任何建议将不胜感激,谢谢!

Close the server and restart it.关闭服务器并重新启动它。 It Works Fine.它工作正常。

npm start or yarn start npm startyarn start

I was having the same issue.我遇到了同样的问题。 You may have a conflict with NPM and YARN.你可能和 NPM 和 YARN 有冲突。 I deleted my yarn.lock and ran it again with only package.lock;我删除了我的 yarn.lock 并只用 package.lock 再次运行它; and it worked.它奏效了。

You should only use Yarn or NPM.你应该只使用 Yarn 或 NPM。

I was stuck on this as well... For me it worked when the version of "react-router" and "react-router-dom" matched.我也被困在这个问题上......对我来说,当“react-router”和“react-router-dom”的版本匹配时,它就起作用了。

 "react-router": "^5.2.0", "react-router-dom": "^5.2.0",

This issue is happening because of the version of react-router-dom.由于 react-router-dom 的版本而发生此问题。 Try updating the react-router-dom to version 6.尝试将 react-router-dom 更新到版本 6。

Just run the command:只需运行命令:

npm install react-router-dom@6.0.0-beta.0
import React, { Component } from 'react';
import Navbar from './components/Navbar'
import { BrowserRouter, Route ,Switch} from 'react-router-dom'
import Home from './components/Home'

class App extends Component {
render() {
  return (
    <BrowserRouter>
     <div className="App">
     <Navbar />
      <Switch>
      <Route exact path='/' component={Home} />
    </Switch>

    </div>
    </BrowserRouter>
   );
  }
  }

  export default App;

try this way试试这个方法

You should add this line to your components.您应该将此行添加到您的组件中。

import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";

My problem was solved by this method.我的问题通过这种方法解决了。

problem solved in me我的问题解决了

npm uninstall react-router

and

npm uninstall react-router-dom

then然后

npm install react-router react-router-dom

use version 5.0.0使用版本 5.0.0

The error occurs because you are using react-router v5 instead of v6-发生错误是因为您使用的是 react-router v5 而不是 v6-

Follow these steps-按着这些次序-

Step 1:步骤1:

npm uninstall react-router

npm uninstall react-router-dom

Step 2:第2步:

npm install react-router@next react-router-dom@next

Step 3: Restart your server第 3 步:重新启动服务器

And you are all set to go!你们都准备好了!

For react-router-dom v6 and greater, use this in your App.js对于 react-router-dom v6 及更高版本,请在您的 App.js 中使用它

import React from 'react'
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'

import '../styles/global.css'

import Layout from '../containers/Layout'
import Home from '../pages/Home'
import Login from '../containers/Login'
import RecoveryPassword from '../containers/RecoveryPassword'
import NotFound from '../pages/NotFound'

const App = () => {
  return (
    <Router>
      <Layout>
        <Routes>
          <Route exact path="/" element={<Home/>}/>
          <Route exact path="/login" element={<Login/>}/>
          <Route exact path="/recovery-password" element={<RecoveryPassword/>}/>
          <Route path="*" element={<NotFound/>}/>
        </Routes>
      </Layout>
    </Router>
  );
}

export default App;

Changes done from v5 to v6从 v5 到 v6 所做的更改

Switch changed to Routes SwitchRoutes

<Route path = '/' component = {Home}></Route>

changed to变成

<Route path = '/' element= {<Home />}></Route>

May be this is happening because you haven not installed it.可能是因为您尚未安装它而发生这种情况。 You should run this command:你应该运行这个命令:

npm i react-router

请重新检查 BrowserRouter 的拼写,否则如果拼写正确,请使用 CTRL + C 重新启动 npm 和

npm start 

像这样安装react-routerreact-router-dom

npm install react-router@next react-router-dom@next

在即将到来的版本中,您的路线似乎已贬值,因此请使用此导入:

import {Route} from 'react-router-dom/Route';

You need to make sure what the return type in App class.您需要确定 App 类中的返回类型是什么。 You are returning BrowserRouter.您正在返回 BrowserRouter。 So when you do an import you need to make sure you put the correct class name which you are importing.因此,当您进行导入时,您需要确保输入要导入的正确类名。

BrowserRouter is being returned not Router.返回的是 BrowserRouter,而不是 Router。

So it should be所以应该是

import { BrowserRouter } from 'react-router-dom'

or或者

import { BrowserRouter as Router } from 'react-router-dom'

I think this may be due to an outdated version being installed.我认为这可能是由于安装了过时的版本。 I was able to run npm install history react-router-dom@next and it fixed my issue.我能够运行npm install history react-router-dom@next并解决了我的问题。

Switch 在较新的版本中已被弃用,使用路由,它会起到相同的作用。

worked by installing:通过安装工作:

npm install react-router-dom@5

in the project folder在项目文件夹中

For me the problem went away after I installe these types:对我来说,安装这些类型后问题就消失了:

npm i @types/react-router-dom

which I found here .我在这里找到的。

I had just recently upgraded to react-router-dom 6.3.0, and that's when the problem began.我最近刚刚升级到react-router-dom 6.3.0,这就是问题开始的时候。

By the way, react-router 6.3.0 will install automatically when you install react-router-dom 6.3.0, no need to install it separately.顺便说一句, react-router 6.3.0 会在你安装react-router-dom 6.3.0 时自动安装,不需要单独安装。

You must delete folder: "node_modules" and write this:您必须删除文件夹:“node_modules”并写下:

npm i react react-dom react-router-dom

It works for me.这个对我有用。

暂无
暂无

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

相关问题 如何修复错误:尝试导入错误:“Navlink”未从“react-router-dom”导出 - How to fix the error: Attempted import error: 'Navlink' is not exported from 'react-router-dom' 尝试导入错误:“Navigate”未从“react-router-dom”导出 - Attempted import error: 'Navigate' is not exported from 'react-router-dom' 尝试导入错误:“useLocation”未从“react-router-dom”导出 - Attempted import error: 'useLocation' is not exported from 'react-router-dom' 尝试导入错误:“出口”未从“react-router-dom”导出 - Attempted import error: 'Outlet' is not exported from 'react-router-dom' 尝试导入错误:“useRouteMatch”未从“react-router-dom”导出 - Attempted import error: 'useRouteMatch' 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' 尝试导入错误:“useHistory”未从“react-router-dom”导出 - Attempted import error: 'useHistory' is not exported from 'react-router-dom' 尝试导入错误:“路由”未从“react-router-dom”导出 - Attempted import error: 'Routes' is not exported from 'react-router-dom' 尝试导入错误:“Switch”未从“react-router-dom”导出 - Attempted import error: 'Switch' is not exported from 'react-router-dom'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM