我正在使用react-router 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);
为什么路由器未定义?
我正在我的一个组件中导入 BrowserRouter 和 Route,该组件仅负责渲染菜单中选定的页面。还有一个 Link 导入到另一个组件中,该组件实际上是一个带有无序列表的 Menu 组件。
如果需要,我将附上所有组件。 感谢您的帮助。
最佳答案
如果你想使用这个
var href = this.context.router.history.createHref(typeof to === 'string' ? { pathname: to } : to);
您需要向您的组件注册路由器,如下所示 在您的组件中放入此代码
static contextTypes = {
router: React.PropTypes.object
}
关于reactjs - 未捕获的类型错误 : Cannot read property 'history' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44515263/