React4.0以上如何獲取當前的路由地址呢


先使用withRouter對組件進行裝飾
然后就可以使用this.props.location.pathname獲取到了
例如:

import React from 'react';
import { withRouter } from 'react-router-dom';

@withRouter
export default class App extends React.Component {
    //...
    getPathname = () => {
        console.log(this.props.location.pathname);
    }
    //...
}

使用@withRouter語法不支持的話使用export default withRouter(App),如下所示

import { Link, withRouter  } from 'react-router-dom'

//組件內容...

  export default withRouter(LeftNav);

詳見文章淺談react傳入路由參數---withRouter組件.

 

.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM