react-router v4中 HashRouter 和 BrowserRouter的使用
遇到的问题
项目中控制路由跳转使用的是BrowserRouter,代码如下:
ReactDOM.render((
<BrowserRouter>
<div className="container">
<Route path={routePaths.INDEX} exact component={Index} />
<Route path={routePaths.CARD} component={Card} />
<Route path={routePaths.BASEINFO} component={BaseInfo} />
<Route path={routePaths.EDUINFO} component={EduInfo} />
<Route path={routePaths.FAMILYINFO} component={FamilyInfo} />
<Route path={routePaths.OTHERINFO} component={OtherInfo} />
<Route path={routePaths.DETAIL} component={Detail}/>
</div>
</BrowserRouter>
),
document.getElementById('app')
);
在开发过程中使用是没有问题的,但是将页面上传至服务器之后,问题就来了:用户访问的资源不存在,页面是空白的。 经过排查怀疑是路径的问题,将BrowserRouter 改为 HashRouter之后,问题解决了~
问题分析
首先分析下出现此问题的原因: 在React项目中我们经常需要采用React-Router来配置我们的页面路由,React-Router 是建立在 history 之上的,常见的history路由方案有三种形式,分别是:
- hashHistory
- browserHistory
- createMemoryHistory
hashHistory 使用 URL 中的 hash(#)部分去创建路由,举例来说,用户访问http://www.example.com/,实际会看到的是http://www.example.com/#/。
<HashRouter>
<div className="container">
<Route path='/repos' component={Repos} />
<Route path='/about' component={About} />
</div>
</HashRouter>
上面代码中,用户访问/repos(比如http://localhost:8080/#/repos)时,加载Repos组件;访问/about(http://localhost:8080/#/about)时,加载About组件。
browserHistory 是使用 React-Router 的应用推荐的 history方案。它使用浏览器中的 History API 用于处理 URL,创建一个像example.com/list/123这样真实的 URL 。
在browserHistory 模式下,URL 是指向真实 URL 的资源路径,当通过真实 URL 访问网站的时候,由于路径是指向服务器的真实路径,但该路径下并没有相关资源,所以用户访问的资源不存在。
Memory history 不会在地址栏被操作或读取。这就解释了我们是如何实现服务器渲染的。同时它也非常适合测试和其他的渲染环境(像 React Native )。和另外两种history的一点不同是你必须创建它,这种方式便于测试。
const history = createMemoryHistory(location)
解决办法
- 使用hashHistory
本地开发时,使用browserHistory是没有问题的,这是因为webpack.config.js中使用 webpack-dev-server 已经做了配置。
webpackConfig.devServer = {
contentBase: path.resolve(__dirname, 'build'),
compress: true, //gzip压缩
historyApiFallback: true,
};
- 如果要使用browserHistory的话,服务器需要进行相关路由配置,方法见 这里;
参考资料:
- https://www.sitepoint.com/react-router-v4-complete-guide/
- http://react-guide.github.io/react-router-cn/docs/guides/basics/Histories.html
- https://www.thinktxt.com/react/2017/02/26/react-router-browserHistory-refresh-404-solution.html
- http://echizen.github.io/tech/2016/07-05-webpack-dev-server-react-router-config
- https://jaketrent.com/post/pushstate-webpack-dev-server/
react-router v4中 HashRouter 和 BrowserRouter的使用的更多相关文章
- React Router V4发布
React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...
- [Web 前端] React Router v4 入坑指南
cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...
- [React Router v4] Render Multiple Components for the Same Route
React Router v4 allows us to render Routes as components wherever we like in our components. This ca ...
- [React Router v4] Conditionally Render a Route with the Switch Component
We often want to render a Route conditionally within our application. In React Router v4, the Route ...
- [React Router v4] Render Catch-All Routes with the Switch Component
There are many cases where we will need a catch-all route in our web applications. This can include ...
- [React Router v4] Intercept Route Changes
If a user has entered some input, or the current Route is in a “dirty” state and we want to confirm ...
- [React Router v4] Redirect to Another Page
Overriding a browser's current location without breaking the back button or causing an infinite redi ...
- [React Router v4] Render Nested Routes
With React Router v4 the entire library is built as a series of React components. That means that cr ...
- [React Router v4] Parse Query Parameters
React Router v4 ignores query parameters entirely. That means that it is up to you to parse them so ...
随机推荐
- spring mybatics
spring boot web.mysql.mybatics.jps 遇到jar包版本不行的直接换个版本Add进去 https://github.com/forezp/SpringBootLe ...
- php使用root用户启动
一般情况下,肯定是不推荐使用root用户启动php的 但是在某些服务器管理想使用WEB的方式来控制操作的话,那么就必须要使用root用户才有权限操作 1.修改配置文件php-fpm.conf的启动用户 ...
- 重写override
不可重写私有方法. 不可重写非静态的方法,虽然编译器不会报错,但是得不到预期的结果. 可以通过重写的形式对父类的功能进行重新定义,比如:对功能进行修改或者进行升级时. class BaseAction ...
- 需求规格说明书(SRS)特点
需求说明书的7大特征: 完整性 正确性 可行性 必要性 划分优先级 无二义性 可验证性 每条需求规格说明书的4大特点: 完整性 一致性 可修改性 可跟踪性 需求管理就是一种获取.组织并记录系统需求的系 ...
- DEV SIT UAT PET SIM PRD PROD常见环境英文缩写含义
英文缩写 英文 中文 DEV development 开发 SIT System Integrate Test 系统整合测试(内测) UAT User Acceptance Test 用户验收测试 P ...
- SkylineGlobe7.0.1版本 支持SQLite(*.sqlite;*.db)数据库
SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 ...
- centos7内网源站建设
centos7内网源站建设 1.部署环境: 系统:Centos7 x86_64 应用服务:nginx.createrepo.reposync 镜像源:https://mirrors.aliyun.co ...
- SpringBoot中各配置文件的优先级及加载顺序
我们在写程序的时候会碰到各种环境(开发.测试.生产),因而,在我们切换环境的时候,我们需要手工切换配置文件的内容.这大大的加大了运维人员的负担,同时会带来一定的安全隐患. 为此,为了能更合理地重写各属 ...
- 在 .NET Core 中结合 HttpClientFactory 使用 Polly(中篇)
译者:王亮作者:Polly 团队原文:http://t.cn/EhZ90oq声明:我翻译技术文章不是逐句翻译的,而是根据我自己的理解来表述的(包括标题).其中可能会去除一些不影响理解但本人实在不知道如 ...
- StatefulSet(一):拓扑状态
Deployment 实际上并不足以覆盖所有的应用编排问题. 造成这个问题的根本原因,在于 Deployment 对应用做了一个简单化假设. 它认为,一个应用的所有 Pod,是完全一样的.所以,它们互 ...