【发布时间】:2016-12-12 07:38:36
【问题描述】:
根据基本要求,我正在尝试实现一个路由,该路由将重定向到另一个路由并向组件添加一个参数。这是为了上传管理。
其实我们有两条路线:
- /documents:显示文档列表
- /documents/upload: 显示文档列表并制作一个带有上传表单的模态框
问题是我在需要保留的 URL 中也有 GET 参数。但是现在,我只想显示在 URL 中的路由 /documents 而不是 /documents/upload,这可能会造成混淆,因为如果我们关闭模式,它就是同一个页面。
所以,我的预期结果是转到 /documents/upload?foo=var&toto=1234 页面会重定向到 /documents?foo=var&toto1234 并保留一个变量来告诉我我需要打开模式。
我已经创建了要重定向的守卫,但似乎我的 queryParams 没有保留,即使将 preserveQueryParams 设置为 true。是问题还是我遗漏了什么?
canActivate(): boolean {
this.documentsService._isUploading = true;
this.router.navigate(['documents'], { preserveQueryParams: true });
return true;
}
还有我的路线定义:
{ path: 'documents', component: DocumentsComponent},
{ path: 'documents/upload', component: DocumentsComponent, canActivate: [UploadRedirectionGuard]}
遇到相同问题的人的最终解决方案:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
this.documentsService._isUploading = true;
this.router.navigate(['documents'], { queryParams: state.root.queryParams });
return true;
}
【问题讨论】:
标签: angular