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

前端 未结 3 2002
暖寄归人
暖寄归人 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:

提交回复
热议问题