官网地址:https://reacttraining.com/react-router/web/api/Route
React Router 4版面以后有巨大的变化 4后的思想是:一切皆组件(路由写成组件)
之前的版本都是传统思想:路由应该统一在一处渲染(类似vue里的路由表)
react-router
:React Router核心APIreact-router-dom
:React Router 的DOM绑定,在浏览器中运行不需要额外安装react-router
react-router-native
:React Native 中使用,实际应用中不会使用这个react-router-config
:静态路由配置React中主要使用
react-router-dom
npm i react-router-dom
react-router-dom
import {BrowserRouter as Router} from "react-router-dom"
as的作用是给一个模块起一个别名
<Router></Router>
<BrowserRouter ></BrowserRouter >
注意:BrowserRouter 需要后端支持
import ReactDom from 'react-dom'
import {BrowserRouter as Router} from "react-router-dom"
import App from "./App.js"
ReactDom.render(
<Router>
<App/>
</Router>,
document.getElementById('root')
)
import {Route,Switch,Redirect} from "react-router-dom"
- Route:是用来通过path路径来匹配对应路径的组件
- Switch:表示一次之渲染一次路由
- Redirect:重定向组件
!!exact 表示路径完全匹配
<Route path="/home" component={Home}></Route>
<Route path="/home" render={()=><Home name={this.state}/>}></Route>
<Route path="/home" children ={()=><Home/>}></Route>
<Switch>
{/* 页面重定向*/}
<Redirect from="/" to="/home" exact></Redirect>
{/* 页面路由 */}
<Route path="/home" component={Home}></Route>
<Route path="/login" component={Login}></Route>
{/* 下面是错误路由匹配404页面 */}
<Route component={NotFount}></Route>
</Switch>
//初始页面
import React,{Component} from "react"
import {Route,Switch,Redirect} from "react-router-dom"
//引入路径
import Home from "../pages/home"
import Login from "../pages/login"
import NotFount from "../pages/not_found"
/**
* Route是用来通过path路径来匹配对应路径的组件
* *path 路由路径
* *component 路由对应组件
* *render 可以渲染一个组件
* *children 子组件
* !!component / render / children 这三个先其中一个使用
*
* Switch表示一次之渲染一次路由
* Redirect重定向组件
* !! exact 表示路径完全匹配
*/
export default class Layout extends Component{
render(){
return(
<div>
<Switch>
<Redirect from="/" to="/home" exact></Redirect>
{/*<Route path="/home" component={Home}></Route>*/}
<Route path="/home" render ={()=>{<Home name={this}/>}></Route>
<Route path="/login" component={Login}></Route>
<Route component={NotFount}></Route>
</Switch>
</div>
)
}
}
就是在对应的一级路由页面下写路由
代码
Home.jsx页面
<Route path="/home/hot" component={Home}></Route>
<Route path="/home/add" component={Home}></Route>
当我们在在一个组件里想用路由属性时,给组件不是路由组件怎么办
这是我们就需用引入withRouter高阶组件
引入方式和使用如下
import { NavLink,Route,Switch,Redirect,withRouter } from 'react-router-dom'
//初始页面
import React,{Component} from "react"
import {Route,Switch,Redirect,withRouter } from "react-router-dom"
//引入路径
import Home from "../pages/home"
import Login from "../pages/login"
import NotFount from "../pages/not_found"
class Layout extends Component{
render(){
return(
<div>
<Switch>
<Redirect from="/" to="/home" exact></Redirect>
{/*<Route path="/home" component={Home}></Route>*/}
<Route path="/home" render ={()=>{<Home name={this}/>}></Route>
<Route path="/login" component={Login}></Route>
<Route component={NotFount}></Route>
</Switch>
</div>
)
}
}
export default withRouter(Layout)//用法
//这样Layout组件就有路由属性了
以上是路由配置
路路由跳转使用需要从react-router-dom 中解构出NavLink
import { NavLink} from 'react-router-dom'
<NavLink to = "/home" activeClassName = "active"> History products </NavLink>
// activeClassName选中时样式为selected
<NavLink
to="/faq"
activeClassName="selected"
>FAQs</NavLink>
// 选中时样式为activeStyle的样式设置
<NavLink
to="/faq"
activeStyle={{
fontWeight: 'bold',
color: 'red'
}}
>FAQs</NavLink>
// 当event id为奇数的时候,激活链接
const oddEvent = (match, location) => {
if (!match) {
return false
}
const eventID = parseInt(match.params.eventID)
return !isNaN(eventID) && eventID % 2 === 1
}
<NavLink
to="/events/123"
isActive={oddEvent}
>Event 123</NavLink>
to={{pathname:`/要跳转的路径`,state:{key:val}}
this.props.location.state.key
this.props.history.push('/child02')
this.props.history.push({pathname:"/home",query:{id:1,name:lisi}})
使用this.props.location.query接收参数