2017-06-14 53 views
0
頁面

在我的應用程序後,如果用戶進入保持URL查詢刷新

http://localhost:3000/#/auth 

的登錄頁面將默認渲染。

當用戶進入註冊頁面,URL是

http://localhost:3000/#/auth?form=signup 

但是,每當我刷新頁面,網址自動回到

http://localhost:3000/#/auth 

而現在用戶看到登錄頁面。

刷新後有沒有辦法保持url查詢?換句話說,如果用戶在註冊頁面上並刷新,我想呈現註冊頁面而不是登錄頁面。我使用[email protected]並使用hashHistory。這裏是路線

<Route path="/auth" component={Auth} /> 
+0

當你說「用戶進入註冊頁面.. https:// localhost:3000 /#/ auth?form-signup」用戶是否真正得到了不同的頁面服務?他不應該......'#'沒有發送到服務器之後的所有內容,所以他應該每次都服務於同一頁面 - http:// localhost:3000 /。 – pbuck

+0

我有一個'Auth'組件,根據查詢呈現不同的表單。它仍然是一樣的頁面。 – davidhu2000

回答

0

我想出了我的問題是什麼。查詢字符串在刷新期間未被清除。我已根據用戶是否在頂層組件中登錄設置了重定向。

_redirect(isLoggedIn) { 
    let route = this.props.location.pathname.slice(1); 

    if (isLoggedIn) { 
     this.props.router.replace('/home'); 
    } else if (!isLoggedIn && !/auth/.test(route)) { 
     this.props.router.replace('/auth'); 
    } 
    } 

我的問題是在else if情況下,我沒有檢查,看看路線包括auth。所以,如果用戶沒有登錄,該功能將重定向到/auth並刪除查詢字符串。

_redirect(isLoggedIn) { 
    let route = this.props.location.pathname.slice(1); 

    if (isLoggedIn && /auth/.test(route)) { 
     this.props.router.replace('/home'); 
    } else if (!isLoggedIn && !/auth/.test(route)) { <== updated this line 
     this.props.router.replace('/auth'); 
    } 
    }