【发布时间】:2017-01-12 15:42:14
【问题描述】:
我想做的是在响应完成后在数组中添加一些数据。 我试图检查响应是否准备好但没有成功:
this.$http.post('/blog/article/' + articleid + '/comment', article_comments)
.then(function (response) {
self.comment_id = response.data.comments.id;
this.user = response.data.comments.user;
this.dataReady = true;
}, function (response) {
});
if (this.dataReady == true) {
this.comments.push({
comment: this.comment,
downvotes: 0,
upvotes: 0,
user:this.user,
date_ago: moment(Date.now()).fromNow()
})
this.loadComments();
console.log(this.comments);
}
我该如何解决这个问题?因为我需要来自响应的数据然后推入数组,否则如果我在响应完成之前尝试推入数组会出错。
【问题讨论】:
-
你可以使用更多的 Promise。如果您返回响应,您可以将
.then与另一个.then(function (response) {...})连接起来。它将使用适当的逻辑顺序进行这些异步调用