2017-09-16 74 views
0

我的問題是類似於React router appending query to URL,但我使用反應路由器4與交換機和路由。在我的情況下,我怎麼能排除從歷史追加額外的查詢參數?反應路由器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')); 

回答

1

回到你的文件(我猜index.js),您正在使用<BrowserHistory />並添加以下內容:

注意,你會搶createBrowserHistory從「歷史」包不是「react-router-dom」 此外「queryKey:false」是答案。

import { BrowserRouter } from 'react-router-dom'; 
import { createBrowserHistory } from 'history'; 

const history = createBrowserHistory({queryKey: false}); 
//..... 

<BrowserRouter history={history}> 
    // Your routes here ... 
</BrowserRouter > 

注:

您可以更好地組織你的路由通過去除確切&倒車所有路線,因爲你正在利用<Switch />組件。

+0

非常感謝。我補充道,但它仍然會在每個api調用中傳遞額外的查詢參數。 as&_ = 1505635138301。我添加了我的索引文件。 –

+0

通過添加緩存修正true.Anyhow你的答案幫助我瞭解路由器和歷史概念的react.Thanks一噸:) –

0

的真正原因不是因爲反應路由器,而是由AJAX API call.I解決這個問題通過張貼Abdennour我的AJAX call.That固定我的答案issue.Anyhow使'cache':true在反應路由器的情況下是正確的。