简体   繁体   English

如何在Angular路由中通过redirectTo获取url片段(锚参数)

[英]How to get url fragments (anchor parameter) through redirectTo in Angular routing

I am trying to do Angular routing redirect through router tables and wondering is there any simple way to have url fragments through the redirect.我正在尝试通过路由器表进行 Angular 路由重定向,想知道是否有任何简单的方法可以通过重定向获取 url 片段。 I have something like this:我有这样的事情:

{
   path: ':word',
   redirectTo: 'blah/:word',
   pathMatch: 'full'
}

When i run this with parameter like localhost:4200/test#fragment Angular redirects it to localhost:4200/blah/test without the fragment.当我使用像 localhost:4200/test#fragment 这样的参数运行它时,Angular 会将它重定向到 localhost:4200/blah/test 而不带片段。 I have managed to bypass this by doing a resolver where I redirect to the right place with fragment, but it feels stupid because then I need to put I dummy component to the router like this:我已经通过做一个解析器来绕过这个,我用片段重定向到正确的地方,但感觉很愚蠢,因为我需要像这样将我的虚拟组件放在路由器上:

{
   path: ':word',
   resolve: { _: RedirectResolver },
   component: DummyComponent, // We never go here
}

If I omit the component, then Angular won't work.如果我省略了该组件,那么 Angular 将无法工作。 Is there any good or native ways to accomplish this kind of behavior?有没有什么好的或原生的方法来完成这种行为?

您可以将“preserveFragment”设置为 true(文档: https : //angular.io/api/router/NavigationExtras

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM