HashLocationStrategy和angular 4中的查询参数 [英] HashLocationStrategy and query parameters in angular 4
问题描述
我正在将现有的angular 4应用程序从PathLocationStrategy迁移到HashLocationStrategy
,并且需要保持入口网址正常工作.看起来像www.test.com/?param1=123
I am migrating an existing angular 4 app from PathLocationStrategy to HashLocationStrategy
and need to keep entry point url working. It looks something like www.test.com/?param1=123
问题在于,一旦我将其切换到HashLocationStrategy,就无法再通过activatedRoute.queryParams访问查询参数. (www.test.com/#/?param1=123
可以正常工作,但我也需要保留原始的url条目)
The problem is that as soon as I switch it to HashLocationStrategy, query params are not accessible through activatedRoute.queryParams anymore. (www.test.com/#/?param1=123
works fine but I need to preserve the original url entry as well)
那么有没有办法用HashLocationStrategy
从www.test.com/?param1=123
获取param1值?我真的不想创建一个空的登录页面,除非我无法避免,否则它将重定向到www.test.com/#/?param1=123
.
So is there a way to get param1 value from www.test.com/?param1=123
with HashLocationStrategy
? I don't really want to create an empty landing page that will redirect to www.test.com/#/?param1=123
unless I can't avoid it.
推荐答案
由于默认已使用HashLocationStrategy
,因此应另外注入PathLocationStrategy
才能获得真实的浏览器位置:
Since HashLocationStrategy
is already used as default, PathLocationStrategy
should be additionally injected in order to get to real browser location:
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);
}
}
}
如果存在基本网址,则应另外将其从路径中删除.
If there is base url, it should be additionally taken away from the path.
这篇关于HashLocationStrategy和angular 4中的查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!