Angular 7-从不同组件更改时重新加载/刷新角度材料数据表

时间:2019-02-21 15:49:16

标签: angular components angular-services angular-components reloaddata

在组件2中进行更改时,如何刷新组件1中的角度材料数据表。这两个组件不在同一父节点下/不相关。

customer.service.ts

export class UserManagementService extends RestService {

  private BASE_URL_UM: string = '/portal/admin';

  private headers = new HttpHeaders({
    'Authorization': localStorage.getItem('token'),
    'Content-Type': 'application/json'
  });

  constructor(protected injector: Injector,
    protected httpClient: HttpClient) {
    super(injector);
  }

  createUser(body: UpdateUserDetailPayload): Observable < UpdateUserDetailPayload > {
    return this.post < UpdateUserDetailPayload >
      (this.getFullUrl(`${this.BASE_URL_UM}/user`), body, {
        headers: this.headers
      });
  }

  getEapGroupDetail(groupCode: string): Observable < EapGroupElement > {
      return this.get < GroupPayload >
        (this.getFullUrl(`${this.BASE_URL_UM}/group/${groupCode`), {
        headers: this.headers
      });
  }
}

component1.ts

onCreateUser() {
  this.userManagementService.createUser(this.setCreatePayload())
    .subscribe(response => {
        console.log('success');
      },
      (error => {
        console.log('error');
      })
    )
}

component2.ts

loadGroupDetail() {
  this.userManagementService.getEapGroupDetail(this.groupCode)
    .subscribe(response => {
      this.groupDetail = response;
      this.dataSource = new MatTableDataSource(this.groupDetail.userList);
      this.dataSource.sort = this.sort;
      this.dataSource.paginator = this.paginator;
    })
}
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">
  <ng-container matColumnDef="username">
    <th mat-header-cell *matHeaderCellDef mat-sort-header class="custom-column">
      {{ 'Dashboard.header.management.user.username' | translate}}
    </th>
    <td mat-cell *matCellDef="let element" class="custom-column">
      {{element.username}}
    </td>
  </ng-container>

  <ng-container matColumnDef="email">
    <th mat-header-cell *matHeaderCellDef mat-sort-header class="custom-column">
      {{ 'Dashboard.header.management.user.email' | translate}}
    </th>
    <td mat-cell *matCellDef="let element" class="custom-column">
      {{element.email}}
    </td>
  </ng-container>

  <tbody>
    <tr mat-header-row *matHeaderRowDef="displayedColumns">
    </tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;">
    </tr>
  </tbody>
</table>

当我在component1中创建新用户onCreateUser()时,loadGroupDetail()应该在component2中刷新。这些组件不相关

1 个答案:

答案 0 :(得分:0)

您可以使用RxJS主题并使用下一个方法来发出更改信号。在用于事件的其他组件中。

conda activate base

添加了简单的示例。如此处的内容:https://medium.com/@benlesh/on-the-subject-of-subjects-in-rxjs-2b08b7198b93