【发布时间】:2018-05-29 20:57:49
【问题描述】:
当我在 Angular 应用程序中导航到带有查询参数的页面时,这些参数最终会消失。
例如,如果我去这里:
http://example.com:8080/TestComponent?OtherName=foo
如果将我重新路由到这里:
http://example.com:8080/TestComponent
因此,由于查询参数被删除,我对ActivatedRoute 的订阅没有返回任何内容。这是我的路线:
import { Routes } from '@angular/router';
import { TestComponent, PageNotFoundComponent } from './exports/components';
export const ROUTES: Routes = [
{
path: 'TestComponent',
component: TestComponent
},
{
path: '**',
component: PageNotFoundComponent
}
];
订阅(route 是ActivatedRoute 的一个实例):
this.route.queryParams.subscribe((params: Params) => {
if (params && Object.keys(params).length > 0) {
const OTHER_NAME = params['OtherName'];
}
});
即使我删除了通配符路径,它仍然会从 URL 中删除参数;因此,它永远不会进入上述if 语句。如何防止查询参数消失?
【问题讨论】:
-
如何进行导航?
-
我只是在地址栏中输入
http://example.com:8080/TestComponent?OtherName=foo。然后Routes数组接管并将我发送到TestComponenthtml。 -
问题在this question中讨论。使用
routerlink或Router.navigate导航时有一些解决方案,但对于初始 URL 或在地址栏中键入 URL 时(据我所知)没有。有一个feature request 允许全局设置保留查询参数。 -
哇,感谢您提供的信息。 :(
-
这不是一个更大的问题吗?这似乎是一个相当普遍的问题......我正在经历非常相似的事情。对我来说,我在哈希之前有一个查询,如下所示:example.com/?data=123#/home。当我去这条路线时,查询被删除。非常令人沮丧,我找不到解决方案。我需要查询来跟踪 Google Analytics 广告系列。
标签: angular parameters routing angular5