react-router应用

react-router

import {Link,Route , Router , IndexRoute , hashHistory, Redirect} from 'react-router';
//Link可以理解为a标签
//暂时未用到Redirect,onEnter 钩子 。Redirect 是一个重定向组件,有 from 和 to 两个属性;onEnter 钩子中用于判断某种情况是否成立,进一步操作某步动作
import React {Component} from 'react';

class Main extends Component{
    render(){
        <div>{this.props.children}</div>
        //Main组件的this.props.children属性就是子组件
    }
}

const route = {
    <Router history={hashHistory}>
        <Route path='/' component={Main}>
        //根路由是'/',参数history,它的值hashHistory表示,路由的切换由URL的hash变化决定,即URL的#部分发生变化,即根路径为xxxxxx.com/#/
            <IndexRoute component={Index}/>
            //Index是根路由的子组件,即指定默认情况下加载的子组件
            <Route path="Menu" component={Menu}/>
            //Route 的 path 属性表示路由组件所对应的路径,可以是绝对或相对路径,相对路径可继承;
            <Route path="About" component={About} />
            <Route path="Article/:id" component={ArticleId} />
            //:id匹配URL的一个部分,直到遇到下一个/、?、#为止。这个路径参数可以通过this.props.params.id取出。
            //URL的查询字符串/?classid=234,可以用this.props.location.query.classid获取
        </Route>
    </Router>
}


在Router组件之外,导航到路由页面,可以使用浏览器的History API
例如 
export class Main extends Component {
constructor(props) {
        super(props);
        this.submit = () => {
            // ...
            hashHistory.push("/")
            //跳转到'/'路径
        }

    }
    render() {    
        return (        
            <div>
                <Link onClick={this.context.router.goBack}>
                    <i>单击返回上一个has路径</i>
                </Link>
                <div onClick={this.submit}></div>
            </div>
        )
    }
}

Main.contextTypes = {
    router: React.PropTypes.object.isRequired
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值