2017-08-21 14 views
0
<Router history={history}> 
     <Switch> 
      <Route exact path="/" render={() => (
       AUTH ? (
        AUTH == "1" ? 
         <NormalPage /> 
         : 
         <AdminPage /> 
       ) : (
         <Redirect to="/login" /> 
        ) 
      )} /> 
      <Route exact path="/login" component={LoginPage} /> 
     </Switch> 
</Router> 

我寫這樣的代碼。當AUTH未定義時,頁面將重定向到登錄頁面。 但是當我登錄時,我應該在登錄頁面中編寫代碼,我想回到主頁。現在,當我登錄時,我使用window.location.href =「/」返回主頁。但我認爲這是不好的方法。提示:我的第一語言不是英語,所以對不起:D當我登錄時,如何重定向到react-router中的主頁?

回答

0

您可以在登錄頁面內使用this.props.history.push("/")。它具有與window.location.href = "/"相同的功能,但是以反應路由器的方式。

相關問題