未捕获类型错误:this.context.router.push不是函数
Uncaught TypeError: this.context.router.push is not a function
正如帖子被调用一样,这就是我从浏览器控制台得到的。单击下拉按钮后,尝试更改到配置文件页面。代码:
contextTypes: {
router: React.PropTypes.func
},
_handleProfile: function(e){
this.context.router.push("profile");
},
<NavDropdown eventKey={3} title={this.props.email} id="collapsible-nav-dropdown">
<MenuItem eventKey={1}>
<Button className="dropdown-button" onClick={this._handleProfile} bsSize="small">
Profile
</Button>
</MenuItem>
<MenuItem divider />
<MenuItem eventKey={2}>
<Button className="dropdown-button" onClick={this._handleLogout} bsSize="small">
Logout
</Button>
</MenuItem>
</NavDropdown>
Using React.createClass,react-bootstrap,react-router
编辑:已添加路线。
var Router = (
<Route name="app" path="/" handler={app}>
<DefaultRoute handler={Index} />
<Route name="login" path="login" handler={Login} />
<Route name="profile" path="profile" handler={Profile} />
</Route>
);
如果您使用的是最新的react路由器v4,则此问题的解决方案需要:
this.context.router.history.push('/path');
新的react router v4在访问this.context.router时更改了推送方法的位置。router现在有历史记录和路由作为属性,您可以在历史记录中找到分配的方法。
这可能不适合旧版本,因为v4已经重写,并且不向后兼容。由于这个问题是第一个出现在谷歌搜索关键字:
Uncaught TypeError: this.context.router.push is not a function
我已经在另一个线程中回答了类似的问题,我决定在这里添加相同的解释,以防未来的开发人员首先出现在这里。
相关文章:
- 混合 ui-sref 和 $state.go 在 Angular ui-router 中进行状态转换
- this.router在AngularJS 2中未定义
- Meteor Router数据函数被调用两次
- NodeJS API调用中Array中的Push和Pull元素
- 如何在router.get()方法中传递url作为参数
- renderReact/Rect Router+Node/Express.js的服务器端/同构渲染中未定义renderP
- 骨干's Router.execute(callback,args,name)方法获取未定义的名称
- 有没有办法用routerLink/router.navigation附加查询/路由参数
- react router router.HistoryLocation以<noscript>但没有完美的工作
- 使用express.具有多个url和可选参数的Router()
- 单击同一链接时,使Iron Router重新加载页面
- React Router一直给我一个警告:你不能更改<路由器路由>
- Push方法显示错误
- BackboneJs Router
- React Router this.context.router.push 不会重新挂载组件
- 未捕获类型错误:this.context.router.push不是函数
- React Router - BrowserHistory.Push VS. ContextTypes
- React Router Push Silently
- React-router:使用 hashHistory.push 刷新子页面内容
- React-router browserHistory.Push不会重定向