路由组件:
路由组件里面是有this.props的。
路由组件:

import React, { Component } from 'react';
import UserUI from '../components/userUI'
class UserPage extends Component{
    handlerBack = () =>{
        console.log(this.props);
        this.props.history.push('/')
    }
    render(){
        return(
            <div>
                我是user页
                <button onClick={this.handlerBack}>回首页</button>
                <UserUI></UserUI>
            </div>
        )
    }
}
export default UserPage;

UI组件:
UI组件里面的this.props是空的,如果要使用的话,需要使用router提供的withRouter组件来完成,也就是要使用一个高阶组件。
UI组件栗子:

import React, { Component } from 'react'
import {withRouter} from 'dva/router'

class UserUI extends Component{
    handlerBack = () =>{
        console.log(this.props)
        this.props.history.push('/')
    }
    render(){
        return(
            <div>
                userUI组件
                <button onClick={this.handlerBack}>返回首页  ---ui</button>
            </div>
        )
    }
}

export default withRouter(UserUI);

觉得对自己有帮助就点个赞?呀。


皈依
6 声望1 粉丝

引用和评论

0 条评论