2017-04-24 67 views
1

我有一个显示的项目列表,编辑和删除按钮角2刷新同一页

的组件如果我点击删除:

onDelete(projectName: string): void { 
    this.projectService.deleteProject(projectName); 

    this.router.navigate(['/projects']); 
} 

我的路由器组成:

export const projectListRouters: Routes = [ 
{ 
    path: '', 
    children: [ 
     {path: '', component: URLComponent}, 
     {path: 'login', component: Login}, 
     {path: 'projects', canActivate: [AuthGuard], component: ProjectListComponent}, 

     {path: 'project/new/-', component: ProjectDetails, canActivate: [AuthGuard], data: {oparationType: 'new'}}, 

     // otherwise redirect to home 
     {path: '**', redirectTo: ''} 
    ], 
}, 

];

我的问题是,this.router.navigate([ '/项目'])不刷新列表中删除项目后,

任何帮助, 感谢

+0

参考:http://stackoverflow.com/questions/41427405/navigate-to-another-page-with-a-button-in-angular-2 –

+0

你可以发表你的组件被路由到何时你去/项目? – snorkpete

+0

我的路由器组件:添加到问题 – Yosefarr

回答

1

我认为你是重定向到同一页面。如果是这样的话,请更新你的模型,说projectList,存储项目名称列表。

if deleteProject is async call,then set this.router.navigate(['/ projects']);在承诺,否则只是从项目清单将删除元素。

希望它有帮助!

+0

更新** projectList **解决了我的问题,谢谢你的帮助 – Yosefarr

0

有现在是刷新组件的方式。您可以创建虚拟组件,重定向到它并重定向回/projects

删除某个项目后,应该调用后端来检索项目列表,就像加载组件时一样。

1

相反,您可以加载您的项目列表订阅服务。

delete() { 

     this._service.delete(this.id). 
      subscribe(x=> { 
       // call your list here. 
       this.projectList(); 
       }, error => { 
       alert(error); 
      }); 
    }