【问题标题】:How to refresh only a part of a component without refreshing ngOnInIt() in angular?如何仅刷新组件的一部分而不刷新 ngOnInIt() 角度?
【发布时间】:2020-02-24 08:28:04
【问题描述】:

我需要知道是否有办法只刷新删除回复部分而不用 ngOnInIt() 刷新整个组件

 public deleteReply(commentId, replyId) {
    this.isLoadingReplyDelete = true;
    this.discussionService.deleteReply(commentId, replyId).takeWhile(() => this.alive)
      .subscribe(returnObj => {
     if (returnObj.status === 200) {
      this.isLoadingReplyDelete = false;
      this.ngOnInit();
    }
   });
  }
 }

【问题讨论】:

  • const index = this.listData.data.indexOf(key); this.listData.data.splice(index, 1);然后你可以调用刷新获取数据api方法..
  • 如果组件的一部分可以自己刷新,那么它应该是当前组件的子组件。
  • 查看角度变化检测器angular.io/api/core/ChangeDetectorRef 并查看这篇文章blog.angular-university.io/…

标签: angular


【解决方案1】:

您不应该从另一个函数的作用域it is part of @angular/core 调用ngOnInit()

另见:https://stackoverflow.com/a/44943103/9766768

您将需要提供一些上下文(例如您的模板代码),看看在这种情况下,ChangeDetectorRef 的哪个实现是好的做法,但其他 cmets 已经为您指明了正确的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 2011-11-24
    • 2012-08-06
    相关资源
    最近更新 更多