我试着用了location、pathname、pathParma都不行,试问怎样才可以获取到呢(路由不带参数)?
4.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);
}
//...
}
在5.1以上版本中,通过Hook可以获得pathname
import { useLocation } from 'react-router-dom'
function Breadcrumb() {
const location = useLocation();
console.log(location.pathname);
return (<div>Path : {location.pathname}</div>);
}
4 回答1.6k 阅读
2 回答1.1k 阅读✓ 已解决
2 回答2.6k 阅读
1 回答958 阅读✓ 已解决
1 回答686 阅读✓ 已解决
2 回答848 阅读✓ 已解决
2 回答1k 阅读