New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[译]React Router:从V2/V3迁移到V4 #4
Comments
很棒 |
写的不错! |
我看了下, 还是有些问题。 |
这个问题很好 @zdJOJO 。
具体差别,还有待研究。 |
thx 我再研究研究 |
看了下bundler-loader var load = require("bundle-loader?lazy!./file.js");
// The chunk is not requested until you call the load function
load(function(file) {
}); 效果和 |
import React from 'react';
import { BrowserRouter, Route, Switch } from 'dva/router';
import dynamic from 'dva/dynamic';
import MiddleSelectPage from "./routes/MiddleSelectPage";
function RouterConfig({ history, app }) {
const IndexPage = dynamic({
app,
component: () => import('./routes/IndexPage'),
});
const HomePage = dynamic({
app,
component: () => import('./routes/Home/HomePage'),
}) ;
return (
<BrowserRouter>
<Switch>
<Route path="/" exact component={MiddleSelectPage} />
<Route path="/Home" exact component={HomePage} />
<Route path="/Login" exact component={IndexPage} />
</Switch>
</BrowserRouter>
);
} 为什么我用了BrowserRouter 浏览器路径中带有#符号,如何设置可以去掉 |
#是hashHistory 或者HashRouter 引起的, 从你的代码看不出问题~ @Chickenbrother |
按需加载不需要集中路由的说话不太正确。按需加载是解决文件大的问题,集中路由是解决架构层级问题。特别是大项目协同开发,没有集中路由还是不太好管理,代码的后期维护也会比较麻烦。 |
@ganxunzou gan那么对于react router v4为了维护架构是否还是要写成集中路由?我觉得如果我们一直写出集中路由的话是不是还是没有真正理由v4里route也是component的概念。 |
@hkjpotato 理论上,v4也是可以写成集中配置的路由的,像以前都写在一起就好了。应该说,更灵活了。 |
@fi3ework @hkjpotato 是可以写集中路由。现在也有 react-router-config 。在没有这个的时候,我借鉴ant-design-pro的方式,自定义router配置,导出所有的Component,然后在使用的地方用Switch控制只能显示同时显示一个。 另外项目如果路由都写到页面里面,后续维护是很麻烦的,结构也不清晰。 |
React Router V4相对V2/V3几乎完全重写了,遵循 Just Component 的 API 设计理念。
react-router V4 分成了三个包:react-router-dom(for web)、react-router-native(for #native)、react-router(core)。在浏览器中,你只需使用react-router-dom。react-router-dom和react-router详见: remix-run/react-router#4648
The Router
在 React Router v3中,有 一个单独的
<Router>
组件. 它会提供一个history
对象作为prop属性。同时,可以使用
routes
prop或者children
为<Router>
配置route
。而在V4中,一个重大变化就是提供了数种不同的router组件。每种都会创建一个
history
对象。<BrowserRouter>
创建browser history
,<HashRouter>
创建hash history
, and<MemoryRouter>
创建memory history
。V4取消了集中的路由配置。
这里要注意的是,router component 只能包裹一个元素。
Routes
在V3中,
<Route>
不是一个真正的组件,而是作为一个标签用来创建route配置对象。使用 v4,您可以像常规的 React 程序一样布局您应用中的组件,您要根据位置(特别是其 pathname )呈现内容的任何位置,您将呈现
<Route>
在 v4,
<Route>
其实是一个组件,所以无论你在哪里渲染<Route>
,它里面的内容都会被渲染。当<Route>
的 path 与当前的路径匹配时,它将会渲染component
,render
, orchildren
属性中的内容,当<Route>
的path
与当前的路径不匹配时,将会渲染null
。路由嵌套
在 v3 中,
<Route>
组件是作为其父<Route>
组件的children
嵌套其中的。当嵌套的
<Route>
匹配时,react 元素将会使用子<Route>
和 父<Route>
的component
属性去构建,子元素将作为父元素的 children 属性。使用 v4,子
<Route>
会由父<Route>
呈现。on* properties
react-router v3 提供
onEnter
,onUpdate
,and onLeave
方法。这些方法本质上是重写(覆盖)了 react 生命周期方法。使用 v4,你将会使用生命周期方法 通过
<Route>
渲染的组件,你可以使用componentDidMount
或componentWillMount
代替onEnter
,你可以使用componentDidUpdate
或者componentWillUpdate
(更或者componentWillReceiveProps
) 代替onUpdate
,你可以使用componentWillUnmount
代替onLeave
。<Switch>
在v3中,您可以指定一些子路由,并且只会渲染匹配到的第一个。
v4 通过
<Switch>
组件提供了相似的功能,当<Switch>
被渲染时,它仅会渲染与当前路径匹配的第一个子<Route>
。<Redirect>
在V3, 如果你想从一个路径重定向到另一个, 例如从
/
到/welcome
, 你可以使用<IndexRedirect >
.在V4,你可以使用
<Redirect>
达到相同的效果。PatternUtils
matchPattern(pattern, pathname)
在v3中,您可以使用与内部相同的匹配代码来检查路径是否匹配模式。在v4中,已被由path-to-regexp 库驱动的matchPath替代。
formatPattern(pattern, params)
在v3中,您可以使用Pattern Utils.format Pattern从路径模式(可能在常量或中央路由配置中)生成有效的路径以及包含名称参数的对象:
Link
在V3中,您可以省略
to
属性或将其设置为null
以创建没有href属性的锚标签。在v4中,您应该总是提供
to
.如果你要设置to
,你可以做对其做一层封装。The text was updated successfully, but these errors were encountered: