我正在使用react-router-dom
来解析一个URL,即我当前的URL
localhost:3000 / q = sldkfjsdlkjflksfd
但是我想要在URL &test=1
localhost:3000 / q = sldkfjsdlkjflksfd&test = 1
const parsed = parse(window.location.search);
答案 0 :(得分:0)
您可以使用HTML Api URLSearchParams
,
componentDidMount(){
let params = new URLSearchParams(this.props.location.search);
let q = params.get('q');
let test = params.get('test');
}
此外,如果未定义this.props.location并且import { withRouter } from 'react-router-dom';
export default withRouter(ComponentName);