react的router路由跳转获取url参数方法

例如一个页面分为三块,导航栏,左栏右栏,左栏为页面跳转,右栏为显示list页面或者detail页面,如下所示:

image

如果使用如下代码进行路由,url改变了,但是接收不到:

1
2
3
4
5
6
7
8
9
10
<div className='content-right'>
<Layout.Content >
<BrowserRouter>
<Switch>
<Route path='/development/list' component={Com_dev_list} />
<Route path="/development/list:type" component={Com_dev_list} />
<Route path='/development/detail:id' component={Com_dev_detail} />
</Switch>
</BrowserRouter>
</Layout.Content>

使用component接收:

1
2
3
4
5
6
7
8
9
10
componentWillReceiveProps(nextProps){
axios.get(host + 'development/list/' + nextProps.match.params.type.replace(":type=","") +'/' +1).then(
response => {
let items = response.data;
items = items.list;
this.setState({list:items});
window.scrollTo(0, 0)
}
);
}

实际上nextProps.match.params.type.replace(“:type=”,””)并未接收到url每次的改变,需要使用window.location.href,这样每次都可以接收到。但是是否真的这么暴力美学,还需要进行之后的学习。

-------------本文结束 感谢您的阅读-------------