响应完成后做些什么?

Do something after response is finished?

我想做的是在响应完成后在数组中添加一些数据。 我正在尝试检查响应是否准备就绪但没有成功:

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){

      });