React的路由据笔者所知有react-router,react-router-dom,笔者认为react-router-dom要比react-router好用很多
一、装包
npm install react-router-dom --save
首先看一下最终的目录结构
目录结构二、在src目录下新建一个Router.js文件用来配置路由
import Page1 from './pages/Page1'
import Page2 from './pages/Page2'
import React, { Component } from 'react'
import { Route, Switch, withRouter, BrowserRouter } from 'react-router-dom'
class Router extends Component {
constructor(props) {
super(props)
this.state = {
}
}
render() {
return (
<BrowserRouter>
<Switch>
<Route exact path="/" component={withRouter(Page1)} />
<Route exact path="/page2" component={withRouter(Page2)} />
</Switch>
</BrowserRouter>
)
}
}
export default Router
三、修改index.js文件
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
// import App from './App';
import Router from './Router'
import * as serviceWorker from './serviceWorker';
ReactDOM.render(<Router />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
四、在页面们中的使用
下边的那种写法都可以,前两种传参的话会暴露在地址栏,第三种比较优雅,不会暴露在地址栏,但是浏览器刷新后参数就没了,需要配合存储使用。
this.props.history.push('/page2')
this.props.history.push({pathname:'/page2'})
this.props.history.push({pathname:"/page2",state : { id : '111' }});
五、路由传参
this.props.history.push('/page2/' + 666 + '')
this.props.history.push({pathname:'/'})
上边的写法传参就是要在后边拼接
不推荐用上边的写法传参,改变URL,可能导致路由失败
this.props.history.push({pathname:"/page2",state : { id : '111' }});
获取数据:this.props.location.state.id
六、效果展示及代码
页面一page1import React, { Component } from 'react'
export class Page1 extends Component {
constructor(props) {
super(props)
this.state = {
};
};
render() {
return (
<div style={{
width: '100%',
height: window.innerHeight,
background: '#001D37',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
}}>
<div
onClick={() => {
// this.props.history.push('/page2/' + 666 + '')
this.props.history.push({pathname:'/page2'})
// this.props.history.push({pathname:"/page2",state : { id : '111' }});
}}
style={{
width: '500px',
height: '200px',
background: '#fff',
padding: '30px',
borderRadius: '10px',
lineHeight: '200px',
textAlign: 'center',
}}>
我是页面一一一一一一一
点我可以去页面二
</div>
</div>
)
}
}
export default Page1
页面二page2
import React, { Component } from 'react'
export class Page2 extends Component {
constructor(props) {
super(props)
this.state = {
};
};
render() {
// console.log(this.props.location.state.id)
console.log(this.props.location.params)
return (
<div style={{
width: '100%',
height: window.innerHeight,
background: '#001D37',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
}}>
<div
onClick={()=>{
this.props.history.push('/')
// this.props.history.push({pathname:'/'})
// this.props.history.push({pathname:"/",state : { id : '222' }});
}}
style={{
width: '500px',
height: '200px',
background: '#ff0',
padding: '30px',
borderRadius: '10px',
textAlign:'center',
lineHeight:'200px',
}}>
我是页面二二二二二二二二
点我可以去页面一
</div>
</div>
)
}
}
export default Page2
欲知知完整代码如何请见GitHub
https://github.com/jade-kylin/react-router-dom-study.git
git clone https://github.com/jade-kylin/react-router-dom-study.git
执行即可获取到完整项目文件
网友评论