BrowserRouter是路由路径不带 #
HashRouter是路由路径带 #
<NavLink activeClassName="样式属性" className="list-group-item">下载模块</NavLink>
<Route path="/demo01" component={Demo01}>
<Route path="/demo02" component={Demo02}>
<Route path="/demo03" component={Demo03}>
....
<Route path="/demo50" component={Demo50}>
<Switch>
<Route path="/demo01" component={Demo01}>
<Route path="/demo02" component={Demo02}>
<Route path="/demo03" component={Demo03}>
....
<Route path="/demo50" component={Demo50}>
<Route path="/demo01" component={Demo0101}>
</Switch>
yarn add react-router-dom
<div classname="list-group">
//在 路由器 BrowserRouter 中配置 路由链接 Link
<BrowserRouter>
<Link className="list-group-item" to="/test01"></Link>
<Link className="list-group-item" to="/test02"></Link>
</BrowserRouter>
</div>
...
<div className="panel">
<div className="panel-body">
<Route path="test01" component={组件名}/>
<Route path="test02" component={组件名}/>
</div>
</div>
浏览器地址栏的对应地址路径如下:
http://localhost:3000/test01
http://localhost:3000/test02
注意:
整个项目必须使用一个路由器,所以在src/index.js中使用路由器把App组件包裹住
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root')
);
路由组件可以接收到路由器自动传入的prop参数,这是路由组件和一般组件的最大区别
一般组件接收的props,是手动传入的,传什么参数,props接收到的是什么参数
<Demo />
<Route path="/demo" component={Demo}
import logo from './logo.svg';
import './App.css';
import MyNavLink from './components/MyNavLink/MyNavLink';
function App() {
return (
<div className="App">
<div className="list-group">
<MyNavLink to="/home" title="主页"}/>
<MyNavLink to="/download" title="下载管理"/>
<MyNavLink to="/user" title="用户管理"/>
</div>
</div>
);
}
export default App;
import React, {Component} from "react"
import {NavLink} from "react-router-dom"
export default class MyNavLink extends Component{
render(){
const {to,title} = this.props
return (
<NavLink activeClassName="lchh" className="list-group-item" to={to}>{title}</NavLink>
)
}
}
<Demo>demo</Demo>
<Demo children="demo"/>
//这两种写法是等价的
import logo from './logo.svg';
import './App.css';
import MyNavLink from './components/MyNavLink/MyNavLink';
function App() {
return (
<div className="App">
<div className="list-group">
<MyNavLink to="/home" children="主页"></MyNavLink>
<MyNavLink to="/download" children="下载管理"></MyNavLink>
<MyNavLink to="/user" children="用户管理"></MyNavLink>
</div>
</div>
);
}
export default App;
import React, {Component} from "react"
import {NavLink} from "react-router-dom"
export default class MyNavLink extends Component{
render(){
return (
<NavLink activeClassName="lchh" className="list-group-item" {...this.props}/>
)
}
}
<NavLink to="/hx/download" activeClassName="test" className="list-group-item">下载模块</NavLink>
<NavLink to="/hx/update" activeClassName="test" className="list-group-item">上传模块</NavLink>
<NavLink to="/hx/change" activeClassName="test" className="list-group-item">修改模块</NavLink>
<NavLink to="/hx/add" activeClassName="test" className="list-group-item">添加模块</NavLink>
<Route path="/hx/about" component={Dwonload}/>
<Route path="/hx/home" component={Update}/>
<Route path="/hx/about" component={Change}/>
<Route path="/hx/home" component={Add}/>
//在 public/index.html文件中我们引入样式css文件
<link rel="stylesheet" href="./css/bootstrap.css">
http://localhost:3000/bootstrap.css
http://localhost:3000/hx/bootstrap.css
这样的路径是错误的,匹配不到
<link rel="stylesheet" href="/css/bootstrap.css">
<link rel="stylesheet" href="%PUBLIC_URL%/css/bootstrap.css">
http://localhost:3000/hx/bootstrap.css
<Route exact={true} path="/demo" to={Demo}/>
或者
<Route exact path="/demo" to={Demo}/>
<Switch>
<Route path="/about" component={About}/>
<Route path="/home" component={Home}/>
<Redirect to="/about"/>
</Switch>
http://localhost:3000/home/message/001/消息001
<Link to={`/home/message/${obj.id}/${obj.title}`}>Message01</Link>
<Route path="/home/message/:id/:title" component={Message}>
const {id,title} = this.props.match.params
http://localhost:3000/home/message/?id=001&title=消息001
<Link to={`/home/message/?id=${obj.id}&title=${obj.title}`}>Message01</Link>
<Route path="/home/message" component={Message}/>
import qs from "querystring"
.....
console.log(this.props.location.search)
//输出结果如下 ?id=001&title=消息01
//去掉字符串第一位的 ?,并转为对象
const obj = qs.parse(this.props.location.search.slice(1))
search: “?id=001&title=消息1” 这种就是urlencoded编码
import qs from "querystring"
let obj = {id:"001", title:"消息01"}
console.log(qs.stringify(obj))
//输出结果 id=001&title=消息01
let strCar = "carName=bmw&price=199"
console.log(qs.parse(strCar))
//输出结果 {carName:"bmw",price:"199"}
http://localhost:3000/home/message
<Link to={{pathname:"/home/message",state:{id: obj.id,title: obj.title}}}>Message01</Link>
const {id,title} = this.props.location.state
const {id,title} = this.props.location.state || {}
<Link replace={true} to={{pathname:"/home/message",state:{id: obj.id,title: obj.title}}}>Message01</Link>
this.props.history.push(path, state) //正常跳转
this.props.history.replace(path,state) // 替换跳转
state 参数是针对使用state传参的方式使用传入参数
一般组件不能够使用路由组件的特有属性 this.props
withRouter(param)是一个函数,
传入参数param是一个普通组件
返回值是一个新的组件,携带有路由组件特定的参数
withRouter作用:
参考: 6路由传参(声明、接收、获取)
import React, { Component } from 'react'
import {withRouter} from "react-router-dom"
class Test extends Component {
render() {
return (
<div>
</div>
)
}
}
export default withRouter(Test)