先使用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组件.

 

.

相关文章:

  • 2022-12-23
  • 2021-06-20
  • 2021-11-27
  • 2021-08-12
  • 2022-12-23
  • 2021-11-05
  • 2021-05-27
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 1970-01-01
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
  • 2022-12-23
相关资源
相似解决方案