react-route4使用

2018-03-12  本文已影响184人  码代码的小公举
import React, { Component } from 'react'
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import {  Route, BrowserRouter, Switch, Link } from 'react-router-dom'

import One from './one'
import Two from './two'

class Car extends Component {
  constructor(props) {
    super(props)
    this.state = {

    }
  }


  render() {
    return (
      <BrowserRouter basename="/car">
        <div>
          <div>
            <Link to="one">to="one" </Link><br />
            <Link to="/two"> to="/two" </Link>
          </div>
          <Switch>
            <Route path="/one" component={One}/>
            <Route path="/two" component={Two}/>
          </Switch>
        </div>
      </BrowserRouter>
    )
  }
}
const mapStateToProps = (state) => {
  return {
  }
}

function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators({
    }, dispatch),
  }
}
export default connect(mapStateToProps, mapDispatchToProps)(Car)


原来的路由是car/one 我们再后面加个a,basename="car"


路由1

点击第一个按钮 to="one",路由的最后a被替换成了one


点击one
再点击第二个按钮 to="/two",basename之后的被替换成了two
再点击two
import React, { Component } from 'react';
import { Provider } from 'react-redux'
import { Route, BrowserRouter, Switch } from 'react-router-dom'    //包
import configureStore from './redux/configureStore'

import Main from './containers/main'         //引入页面
import Car from './containers/car'
import Wallet from './containers/wallet'
import User from './containers/user'
const store = configureStore()              //页面store

export default class App extends Component {
  constructor(props) {
    super(props)
  }

  render() {
    //写router
      return (
        <Provider store={store}>
          <BrowserRouter>
            <Switch>
              <Route exact path="/" component={Main}/>
              <Route path="/wallet" component={Wallet}/>
              <Route path="/car" component={Car}/>
              <Route path="/user" component={User}/>
            </Switch>
          </BrowserRouter>
        </Provider>
      )
  }
}


import React, {Component} from 'react';
import { BrowserRouter } from 'react-router-dom'

export default class Main extends Component {
  render() {
    return (
      <BrowserRouter>
        <div className="root">
          <div>
              <button onClick={() => this.props.history.push('/car')}>car</button>
              <br/>
              <button onClick={() => this.props.history.push('/wallet')}>wallet</button>
              <br/>
              <button onClick={() => this.props.history.push('/user')}>User</button>
              <br/>
          </div>
        </div>
      </BrowserRouter>
    )
  }
}

this.props.history.push('/car')
this.props.history.go()
import React, {Component} from 'react';
import { BrowserRouter, Switch, Route } from 'react-router-dom'

import BindPhone from './bindPhone'

import './style.less'

export default class Wallet extends Component {
  render() {
    return (
      <BrowserRouter>
        <div>
          <div style={{textAlign:'center'}}>我在写router前</div>
          <Switch>
            <Route exact path="/user" component={BindPhone}/>
          </Switch>
        </div>
      </BrowserRouter>
    )
  }
}
上一篇 下一篇

猜你喜欢

热点阅读