Angular2 router keep query string

前端 未结 9 1745
栀梦
栀梦 2020-11-29 01:52

I wrote an Angular2 (v2.0.1) application that makes use of the router. The website is loaded with several query string parameters, so the full URL initially looks like this:

9条回答
  •  隐瞒了意图╮
    2020-11-29 02:12

    It turns out the undocumented way to do this without other hacks is to simply remove the leading slash in the "redirectTo" field. Since you are matching the full path you can have the certainty that it'll do what you want (i.e. no surprise url segments) and since it's no longer an absolute target, Angular will preserve the current query parameters.

    So in this case

    {
      path: '',
      redirectTo: '/comp1',
      pathMatch: 'full'
    }
    

    becomes:

    {
      path: '',
      redirectTo: 'comp1',
      pathMatch: 'full'
    }
    

    Source: https://github.com/angular/angular/issues/13315

提交回复
热议问题