Javascript React路由器不显示浏览器历史记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44063229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:19:00  来源:igfitidea点击:

React router not showing browser history

javascriptreactjsreact-router

提问by stackjlei

I'm learning from this tutorialbut I keep getting this error:

我正在从本教程中学习,但我不断收到此错误:

'react-router' does not contain an export named 'browserHistory'.

“react-router”不包含名为“browserHistory”的导出。

The file that has react-router is this:

具有反应路由器的文件是这样的:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, browserHistory } from 'react-router';
import routes from './routes';

ReactDOM.render(
  <Router history={browserHistory} routes={routes} />, 
  document.getElementById('root')
);

回答by Balthazar

You need to get browserHistoryfrom the historymodule now.

您现在需要browserHistory历史模块中获取。

import createHistory from 'history/createBrowserHistory'

Note that they changed the module API recently so if you are using the latest version the import slightly changed:

请注意,他们最近更改了模块 API,因此如果您使用的是最新版本,导入会略有变化:

import { createBrowserHistory } from 'history'

回答by Kv B

I had the same problem and I wasted a couple of days to figure it out. This error happens simply because react-router v4 does not have the browserHistory(I don't know if that's a good thing or not though). I solved the issue by installing v3 like this:

我遇到了同样的问题,我浪费了几天时间才弄清楚。发生此错误的原因很简单,因为 react-router v4 没有browserHistory(我不知道这是否是一件好事)。我通过像这样安装 v3 解决了这个问题:

npm install react-router@3 --save

回答by DalSoft

You are using version 4 of react-router.

您正在使用 react-router 的第 4 版

Either downgrade the package or follow the instructions in this SO answerto make it work with v4.

降级软件包或按照此SO 答案中的说明进行操作以使其与 v4.x 一起使用。

回答by Shashwat Gupta

Simple Solution

简单的解决方案

method 1:

方法一:

npm install --save history

use this now:

import createHistory from 'history/createBrowserHistory'

method:2

方法:2

Use Version 3  

npm install react-router@3

回答by Kiran Maniya

You are following implementation os v3 react-router. Use the Version 3 and install again as

您正在关注实现 os v3 react-router。使用版本 3 并再次安装为

npm install react-router@3

you will be up and running. if you want to use v4, you have to implement it accordingly.

你会启动并运行。如果你想使用 v4,你必须相应地实现它。