我的问题类似于 React router appending query to URL但我正在使用带有 Switch 和路由的 React router 4。就我而言,如何排除从历史记录中附加的额外查询参数?
render(){
return(
<div>
<Header/>
<Switch>
<Route exact path="/" render={(props) => (<Home passDealsToRoute ={this.dealsCallBack} {...props}/>)}/>
<Route exact path="/mall/:id/:store_id/:deal_id" render={(props) => (<MallDetail deals ={this.deals} {...props}/>)}/>
<Route exact path="/mall/:id/:store_id" render={(props) => (<MallDetail deals ={this.deals} {...props}/>)}/>
<Route exact path="/mall/:id" render={(props) => (<MallDetail deals ={this.deals} {...props}/>)}/>
<Route path="/home/:location" render={(props) => (<Home passDealsToRoute ={this.dealsCallBack} {...props}/>)} />
<Route exact path="/about" component={About}/>
</Switch>
<Footer/>
</div>
);}} export default withRouter(Root);
我的索引文件
import React from 'react';
import {BrowserRouter,Route} from 'react-router-dom';
import {render} from 'react-dom';
import Root from './components/root/root';
import { createBrowserHistory } from 'history';
const history = createBrowserHistory({queryKey: false});
class App extends React.Component {
render() {
return(
<BrowserRouter history={history}>
<Route path={"/"} component={Root}/>
</BrowserRouter>
);
}
};
render(<App/>,window.document.getElementById('app'));
最佳答案
返回到您正在使用 index.js
的文件(我猜 <BrowserHistory />
)并添加以下内容:
Note that you will grab
createBrowserHistory
from "history" package not "react-router-dom" Also "queryKey: false" is the answer here .
import { BrowserRouter } from 'react-router-dom';
import { createBrowserHistory } from 'history';
const history = createBrowserHistory({queryKey: false});
//.....
<BrowserRouter history={history}>
// Your routes here ...
</BrowserRouter >
注意:
由于您正在利用<Switch />
,因此您可以通过删除精确的并反转所有路线来更好地组织您的路线。组件。
关于javascript - React router 4 附加额外的查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46255410/