【问题标题】:Refresh component after http posthttp post后刷新组件
【发布时间】:2017-01-12 13:28:25
【问题描述】:

我有一个添加新项目的模态(addProjectModal 组件)

save(data:Project) {
  data.customer_id = this.customerID;
  data.supervisor_id = 450;
  this._projectService.addProject(data)
    .subscribe(res => console.log(res)); //maybe init the refresh here
}

这个组件用在我保存项目数据的projectComponent中

ngOnInit() {
  this.collapsedItem = -1;
  this._projectService.getProjects(this.customerID)
    .subscribe(data => {
      this.projects = data
    });
}

你会如何告诉父组件刷新(从数据库请求新数据)?

【问题讨论】:

    标签: angular angular2-services


    【解决方案1】:

    我会说使用

    @Output()

    为此...

    因此 html 代码将如下所示:

    <parentComponent>
         <childComponent tellParent="parentFunction()"></childComponent>
    </parentComponent>
    

    【讨论】:

    • 谢谢!使用共享服务(单例)可能更好吗?是否可以在注入服务之前在项目服务中填充共享项目数组(pout of database)?
    • 使用共享服务是个好主意,但是如果您在子组件中没有任何用途,那么想要在子组件中添加服务引用等等,但是可以是服务是另一种选择
    • 我在两个组件中都使用了该服务...但还没有作为单例
    • 使用单音
    猜你喜欢
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 2017-07-31
    相关资源
    最近更新 更多