2017-06-13 139 views
2

財產「歷史」我使用的反應路由器V4.1.1,當我嘗試使用鏈接組件 - 我得到一個警告:遺漏的類型錯誤:無法讀取的不確定

Failed context type: The context `router` is marked as required in `Link`, 
but its value is `undefined`. 

,並在錯誤:

Uncaught TypeError: Cannot read property 'history' of undefined 

錯誤是在此字符串鏈接組件:

var href = this.context.router.history.createHref(typeof to === 'string' ? { pathname: to } : to); 

爲什麼路由器是不確定的?

我導入瀏覽器路由器和路由在我的組件之一,只負責在菜單中呈現選定的頁面。另一個組件導入鏈接,實際上它是一個帶有無序列表的Menu組件。

如果需要,我會附上我所有的組件。 謝謝你的幫助。

+0

請附上組件代碼如下 註冊路由器。你的菜單組件應該有一個父路由組件。 –

+0

哦。包含Menu組件的Routes和Header是兄弟節點。這是解決問題的唯一方法嗎?這將改變畝應用程序的結構。 –

+0

您的菜單組件應由路由器組件包裝。這是您的鏈接工作的唯一要求。 –

回答

7

,如果你想用這個

var href = this.context.router.history.createHref(typeof to === 'string' ? { pathname: to } : to); 

你需要與你的組件在你的組件把這個代碼

static contextTypes = { 
    router: React.PropTypes.object 
} 
相關問題