单击角度中的删除按钮时页面未刷新

ash*_*rki 2 javascript typescript angular angular6

我有一个页面运行在“ http://localhost:4200/assignmentForAudit ”,用户界面看起来像在此输入图像描述

当我单击删除按钮时,数据将被删除,但页面不会刷新。我试着把 this.router.navigate(['/assignmentForAudit']);在删除操作后放置,但它没有刷新页面。我怎样才能实现这种刷新方法以便删除数据?

在component.ts中删除的方法

onDelete(nas:any){
   this.assignmentAudit.id=nas.assignmentAudit[0].id;
   console.log(this.assignmentAudit.id);
  if(window.confirm('Are sure you want to delete this item ?')){
     this.assignmentAuditService.deleteGroupFromSelection(this.assignmentAudit.id)
      .subscribe(
        data => {
          console.log(data);
        },
        error => console.log(error));
   }
    this.router.navigate(['/assignmentForAudit']);

}
Run Code Online (Sandbox Code Playgroud)

assignment-audit.service.ts类方法调用api操作

deleteGroupFromSelection(id: number): Observable<any> {
    return this.http.delete(`${this.baseUrl}/${id}`, { responseType: 'text' });
  }
Run Code Online (Sandbox Code Playgroud)

删除操作工作正常,但问题是页面不刷新。

Ami*_*wzy 5

当您使用角度时,根本不建议将其作为最佳实践,您可以data list在每次delete更新数据后简单地发出可视化数据,但如果您想要这样的行为。

第一种情况是重新加载整个页面,您可以在每次之后这样做delete

window.location.reload();
Run Code Online (Sandbox Code Playgroud)

第二种情况,如果您只需要重新加载组件,您可以解决这个问题并通过黑客实现它(只是为了欺骗您component导航离开并再次导航到它)

this.router.navigateByUrl('/DummyComponent', {skipLocationChange: true}).then(() => this.router.navigate(['Your actualComponent you want to reload']));
Run Code Online (Sandbox Code Playgroud)

/DummyComponent可能是一个空组件,您只需使用它来欺骗您需要刷新/重新加载的实际组件