npm install -g create-react-app 全局环境
html
create-react-app my-app 建立项目
react
cd my-app 进入项目
ios
npm start 启动
npm
路由是根据不一样的 url 地址展现不一样的内容或页面。
复制代码
一个针对React而设计的路由解决方案、能够友好的帮你解决React components 到URl之间的同步映射关系。
复制代码
npm uninstall react react-dom --save-dev
复制代码
npm i react@15 react-dom@15 react-router@2 axios --save-dev
复制代码
import React from 'react'
import ReactDOM from 'react-dom'
import { Router, Route, Link, hashHistory} from 'react-router'
复制代码
定义连接的组件,相似于a标签。 <Link to='/users'>users</Link>
编程
{this.props.children}
==至关于路由试图的容器axios
renderbash
(<Router history={hashHistory}>
<Route path=”/" component={Demo}> <Route path="/home" component={Header}></Route> <Route path="/about" component={Con}></Route> </Route> </Router>, document.getElementById('root')) 复制代码
<Route>
组件有以下属性:react-router
指定默认状况下加载的子组件。你能够把IndexRoute想象成某个路径的index.html。app
例如:dom
<Route path="/" component={App}>
<IndexRoute component={Index}/>
</Route>
复制代码
const ACTIVE = { color: 'red' }
复制代码
<Link to="/users" activeStyle={ACTIVE}>users</Link>
复制代码
<Link to=“/users/1>users</Link>
<Route path="/user/:xxxx" component={User}/>
复制代码
this.props.params.xxxx==1
hashHistory.push('/home')
<Route path="inbox" component={Inbox}>
<Route path="messages/:id" component={Message} />
</Route>
复制代码
<Route path="inbox" component={Inbox}>
<Route path=“/messages/:id" component={Message} /> </Route> 复制代码
<Route path=”/index" component={index}> <Redirect from=”/index/a" to=“/other" /> </Route> 复制代码
从 /index/a 跳转到 /other
重定向
<IndexRedirect to="/home"/>
复制代码
查询符-query 定义:
<Route path="/user/:xxxx" component={User}/>
复制代码
取得参数: this.props.params.xxxx
例如:
<Link to={{pathname:'/list',query:{id:item.goodsID} }}>
<Route path="/user" component={User}/>
复制代码
url:/user/10086?foo=bar
this.props.params.userId 是 10086
this.props.location.query.foo 是 bar
复制代码