请教各位师兄了。我创建了一个组件Component,并在内部中的一个ajax成功回调内,写了this.context.router.push("/user/list")类似的跳转功能。同时在组件外写了Component.contextTypes={ router: React.PropTypes.object.isRequired }。ajax也成功请求了,但是页面并没有跳转,有点疑问了。。。代码结构类似:class Component extends React.Component{ ... success: function(data) { alert(data); this.context.router.push(...) }}Component.contextTypes={ router: React.PropTypes.object.isRequired}
1 回答

潇湘沐
TA贡献1816条经验 获得超6个赞
"Cann't read the property 'push' is not defined"
这个错误确保contextTypes写好了
并且构造函数调用super是没有把context弄丢
class Component {
constructor(props, context) {
super(...arguments) // 这样才行,如果只写props, 会把context 弄丢,所以super时始终建议这么写
}
}
添加回答
举报
0/150
提交
取消