簡體   English   中英

this.props.history.push不起作用

[英]this.props.history.push not working in react

我試圖使用App.jsx中的歷史記錄推送進行導航。 我用下面的代碼相同。

    componentDidMount() {
        if(!this.authService.isAuthenticated())
        {           
             this.props.history.push('/Login');
        }
    }

但是,由於歷史記錄未定義,因此出現錯誤。 我嘗試記錄道具對象,並得到以下輸出。

在此處輸入圖片說明

請幫助我瀏覽app.jsx。

使用withRouter導出組件。

用路由器導入。

import { withRouter } from 'react-router'

像這樣導出您的組件。

export default withRouter(MyComponent)//your component name 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM