【发布时间】:2018-05-22 16:35:15
【问题描述】:
我正在使用 vuejs 开发一个应用程序。我想在 Promise 中使用 this,我希望能够在 .then 范围内调用函数或数据,为此我使用 this。但是,如果我在 .then 中执行此操作,则会出现错误,即使我将范围与箭头函数绑定,它似乎也会丢失。我能做什么?
javascript
methods: {
...mapActions(['setCredentials']),
doLogin() {
console.log('credentials ' + this.username, this.password);
this.$store.dispatch('connect', {
username: this.username,
password: this.password,
});
this.checkResult().then((interval) => {
vm.$f7router.navigate('/favorites');
})
},
checkResult() {
return new Promise(function(resolve) {
var id = setInterval( () => {
let condition = this.$store.state.isConnected
if (condition) {
clearInterval(id);
resolve(id);
}
setTimeout(() => {
clearInterval(id);
reject(id);
}, 20000);
}, 10);
});
}
【问题讨论】:
标签: javascript vue.js promise