一、react-router 4.x
先使用withRouter对组件进行装饰
然后就可以使用this.props.location.pathname获取到了
使用@withRouter语法不支持的话使用export default withRouter(App)
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);
}
//...
}二、react-router 5.x
import { useLocation } from 'react-router-dom'
function Breadcrumb() {
const location = useLocation();
console.log(location.pathname);
return (<div>Path : {location.pathname}</div>);
}版权声明:本文为qq_31851435原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。