问题描述
mobx的store中有一个获取token的request,如果获取成功将跳转路由至'/',但是store中无法使用this.props.history.push('/')
问题出现的环境背景及自己尝试过哪些方法
尝试加一个state存储登录与否,但是不太好,有没有直接解决的方法?
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
class IssueStore {
@observable open_list = []
@observable item = ''
@observable isLogin = false
@observable username = ''
@observable password = ''
@observable table_loading =true
@computed get fullName() {
const { firstName, lastName } = this;
if (!firstName && !lastName) {
return "Please input your name!";
} else {
return firstName + " " + lastName;
}
}
@computed get loginStatus(){
return loginStatus=this.isLogin;
}
@action queryToken() {
NewRequest.request({
form: {
"username": this.username,
"password": this.password
},
url: BaseUrl+'/api/api-token-auth/',
success: function (res) {
let str = JSON.parse(res);
NewRequest.setStorage('token', str.token);
NewRequest.setStorage('username',this.username );
this.isLogin=true
}.bind(this),
error: function () {
message.error('信息错误,请重新登录!');
}.bind(this)
})
}
@action queryOpenList() {
NewRequest.request({
user_method: 'List_open',
url: BaseUrl + '/api/issue/',
success: function (res) {
var obj = JSON.parse(res);
this.open_list=obj;
this.table_loading=false;
}.bind(this),
error: function (res) {
if(res.status==='401'){
}
}.bind(this)
})
}
}
你期待的结果是什么?实际看到的错误信息又是什么?
希望能够直接在上面这个queryToken函数里直接进行路由跳转。
跳转的逻辑放到组件里进行吧