gpt4 book ai didi

angular - HashLocationStrategy 和 Angular 4 中的查询参数

转载 作者:行者123 更新时间:2023-12-04 03:09:30 28 4
gpt4 key购买 nike

我正在将现有的 Angular 4 应用程序从 PathLocationStrategy 迁移到 HashLocationStrategy 并且需要保持入口点 url 正常工作。它看起来像 www.test.com/?param1=123

问题是,一旦我将其切换到 HashLocationStrategy,查询参数就无法再通过 activatedRoute.queryParams 访问。 (www.test.com/#/?param1=123 工作正常,但我还需要保留原始 url 条目)

那么有没有一种方法可以使用HashLocationStrategywww.test.com/?param1=123 获取param1 值?我真的不想创建一个将重定向到 www.test.com/#/?param1=123 的空着陆页,除非我无法避免。

最佳答案

由于 HashLocationStrategy 已被用作默认值,因此应额外注入(inject) PathLocationStrategy 以获取真实的浏览器位置:

  providers: [
PathLocationStrategy,
{provide: LocationStrategy, useClass: HashLocationStrategy},
...
]

...

class AppComponent {
constructor(router: Router, pathLocationStrategy: PathLocationStrategy) {
const basePath = pathLocationStrategy.getBaseHref();
const absolutePathWithParams = pathLocationStrategy.path();

if (basePath !== absolutePathWithParams) {
router.navigateByUrl(absolutePathWithParams);
}
}
}

如果有base url,需要另外从路径中去掉。

关于angular - HashLocationStrategy 和 Angular 4 中的查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46396572/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com