2016-01-28 36 views
3

後,我有我的動作的成功執行後,像終極版過渡到操作執行

export function loginSuccess(response){ 
    return (dispatch, getState) => { 
    dispatch({ response, type: types.LOGIN }); 
    router.transitionTo("/profile") 

    }; 
} 

這裏的行動,我想它重定向到/profile

這裏我得到router is not defined

+0

'router'應該傳入'this.props'。 –

+0

這是在我的行動..我怎麼能通過this.props .. – aryan

+0

啊,沒有仔細閱讀。你使用'redux-router'嗎? –

回答

4

你必須要路由器傳遞給你的終極版咚行動的創建者(你的代碼PICE不是一個動作,但我寫行動的創建者)。例如,它可能看起來像:

export function loginSuccess(router, response){ 
    return (dispatch, getState) => { 
    dispatch({ response, type: types.LOGIN }); 
    router.transitionTo("/profile") 

    }; 
} 

您是否有權訪問您的路由器以調用此操作創建者?它在那裏可用嗎?如果是的話那麼你有沒有這樣的事情(我不知道什麼是response):

this.props.dispatch(loginSuccess(response)); 

,你必須將其更改爲:

this.props.dispatch(loginSuccess(this.context.router, response)); 

我假設你可以訪問路由器通過上下文。

因爲你不顯示你如何調用這個動作,我只是猜測。我希望我的指導能幫助你。

還有一件事。新的react-router api在路由器中沒有transitionTo方法!我不知道你是否使用新的或舊的API。但是,如果你得到錯誤,如:

調用transitionTo失敗: 'location.search.substring不是一個函數'

隨後還更改:

router.transitionTo("/profile") 

到:

router.push("/profile") 
+0

事情是我我正在從我的組件調用這個動作,而我沒有得到'this'對象是動作..你能幫忙嗎? – aryan

+0

是的,在你的問題中寫下你的組件內容(如果可能的話刪除所有過時的代碼)。 –

+0

但如果我不得不猜測更多。然後,您嘗試在調用回調的方法中調用您的操作。你可以嘗試添加構造函數: 'this.yourMethodWhichInvokeAction = this.yourMethodWhichInvokeAction.bind(this)' –

1

可以使用history外部組件來提供當前路由

import { browserHistory } from 'react-router' 

export function loginSuccess(response){ 
    return (dispatch, getState) => { 
    dispatch({ response, type: types.LOGIN }); 
    browserHistory.push("/profile") 
    }; 
} 
+0

它給結果accint到瀏覽器的歷史..我不想要這個......這將是如此的好,如果你能解釋我怎麼可以這樣做以redux方式像router.transitionTo(「/ profile」) – aryan