首先从react-router的npm包开始看起,分别有react-router和react-router-dom,二者的区别在于后者具备一些dom api,好比Link、BrowserRouter、HashRouter。不难发现react-router-dom具备一些在浏览器上所须要的api,而react-router只有核心api,因此咱们总结下javascript
其中react-router-dom和react-router-native都继承自react-router,因此咱们在浏览器上开发只须要引入react-router-dom便可
vue
我最早接触的框架是vue,vue中的路由配置使用的是配置文件,而react使用的是jsx来配置路由,因此初次学习会有点别扭。java
其实就是路由的hash和history两种模式(要是不了解这两种模式之间的区别那就须要去恶补下啦)
而且这两个组件是路由的容器,必须在最外层react
// hash模式
ReactDom.render(
<HashRouter>
<Route path="/" component={Home}/>
</HashRouter>
)
// history模式
ReactDom.render(
<BrowserRouter>
<Route path="/" component={Home}/>
</BrowserRouter>
)
复制代码
下面说说HashRouter和BrowserRouter上的参数web
Prompt是用来提示用户是否要跳转,给用户提示信息默认使用window.confirm,能够结合getUserConfirmation构建自定义提示信息vue-router
<Prompt message={location => {
return '请确认'
}}/>
若是直接返回true,则不会弹窗
复制代码
Route是路由的一个原材料,它是控制路径对应显示的组件npm
Route的参数编程
path | url | 是否开启 | 匹配结果 |
---|---|---|---|
/a | /a/b | false | yes |
/a | /a/b | true | no |
path | url | 是否开启 | 匹配结果 |
---|---|---|---|
/a | /a | true | yes |
/a | /A | true | yes |
path | url | 是否开启 | 匹配结果 |
---|---|---|---|
/a | /a/ | true | yes |
/a | /a/c | true | yes |
/a | /a | true | no |
低级路由,适用于任何路由组件,主要和redux深度集成,使用必须配合history对象
使用Router路由的目的是和状态管理库如redux中的history同步对接redux
<Router history={history}>
...
</Router>
复制代码
二者都是跳转路由,NavLink的参数更多些api
<Link to="/a"/>
复制代码
<Link to={{
pathname: '/courses',
search: '?sort=name',
hash: '#the-hash',
state: { fromDashboard: true }
}}/>
复制代码
const oddEvent = (match, location) => {
console.log(match,location)
if (!match) {
return false
}
console.log(match.id)
return true
}
<NavLink isActive={oddEvent} to="/a/123">组件一</NavLink>
复制代码
<NavLink to="/a/123" location={{ key:"mb5wu3", pathname:"/a/123" }}/>
复制代码
Redirect重定向很简单,咱们直接看代码便可
// 基本的重定向
<Redirect to="/somewhere/else" />
// 对象形式
<Redirect
to={{
pathname: "/login",
search: "?utm=your+face",
state: { referrer: currentLocation }
}}
/>
// 采用push生成新的记录
<Redirect push to="/somewhere/else" />
// 配合Switch组件使用,form表示重定向以前的路径,若是匹配则重定向,不匹配则不重定向
<Switch>
<Redirect from='/old-path' to='/new-path'/> <Route path='/new-path' component={Place}/> </Switch> 复制代码
路由切换,只会匹配第一个路由,能够想象成tab栏
Switch内部只能包含Route、Redirect、Router
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
<Route path="/:user" component={User}/>
<Route component={NoMatch}/>
</Switch>
复制代码
当一个非路由组件也想访问到当前路由的match,location,history对象,那么withRouter将是一个很是好的选择,能够理解为将一个组件包裹成路由组件
import { withRouter } from 'react-router-dom'
const MyComponent = (props) => {
const { match, location, history } = this.props
return (
<div>{props.location.pathname}</div>
)
}
const FirstTest = withRouter(MyComponent);
复制代码
用过vue的都知道,vue-router有组件形式的导航,也有编程式导航,那react-router怎么使用api来控制前进后退和刷新呢?这就须要咱们来讲明下history对象的做用了
其实在每一个路由组件中咱们可使用this.props.history获取到history对象,也可使用withRouter包裹组件获取,
在history中封装了push,replace,go等方法,具体内容以下
History {
length: number;
action: Action;
location: Location;
push(path: Path, state?: LocationState): void; // 调用push前进到一个地址,能够接受一个state对象,就是自定义的路由数据
push(location: LocationDescriptorObject): void; // 接受一个location的描述对象
replace(path: Path, state?: LocationState): void; // 用页面替换当前的路径,不可再goBack
replace(location: LocationDescriptorObject): void; // 同上
go(n: number): void; // 往前走多少也页面
goBack(): void; // 返回一个页面
goForward(): void; // 前进一个页面
block(prompt?: boolean | string | TransitionPromptHook): UnregisterCallback;
listen(listener: LocationListener): UnregisterCallback;
createHref(location: LocationDescriptorObject): Href;
}
复制代码
这样咱们想使用api来操做前进后退就能够调用history中的方法啦