Vuejs2 在响应完成后做些什么?
我试图做的是在响应完成后在数组中添加一些数据。 我正在尝试检查Response是否已准备就绪,但未成功:Vuejs2 在响应完成后做些什么?,vuejs2,vue.js,Vuejs2,Vue.js,我试图做的是在响应完成后在数组中添加一些数据。 我正在尝试检查Response是否已准备就绪,但未成功: 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.$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);
}
我怎样才能解决这个问题?因为我需要从响应中获取数据,然后推入数组,否则,如果我在响应完成之前尝试推入数组,我将得到一个错误。您可以将以下代码放入一个方法中,在获取响应后执行这些代码:例如
updateOtherVars
:
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);
}
并从this.$http
块调用此方法,如下所示:
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;
this.updateOtherVars() //Call method from here
},function(response){
});
你可以用更多的承诺。如果返回响应,则可以将
.then
与另一个.then(函数(响应){…})
连接起来。它将使用正确的逻辑顺序进行这些异步调用