How to keep query string parameters in URL when accessing a route of an Angular 2 app?

前端 未结 3 1966
暖寄归人
暖寄归人 2021-01-03 08:28

I have an Angular 2 test app running the latest alpha (37). There are just three routes, that look like this:

@RouteConfig([
  { path: \'/\', component: Home         


        
相关标签:
3条回答
  • 2021-01-03 09:18

    Updating for Angular 2:
    To preserve query parameters present in the current url, add

    // import NavigationExtras 
    import  { NavigationExtras } from '@angular/router';
    
    // Set our navigation extras object
    // that contains our global query params
    
    let navigationExtras: NavigationExtras = {
          preserveQueryParams: true
        };   
    
    // Navigate to the login page with extras
    this.router.navigate(['/someLink'], navigationExtras);
    

    For more info, check here:

    0 讨论(0)
  • 2021-01-03 09:19

    This is fixed in Alpha 41. The query string is now persisted.

    0 讨论(0)
  • 2021-01-03 09:20

    Try this this.router.navigate(['target'], {preserveQueryParams: true});

    0 讨论(0)
提交回复
热议问题