Angular2 router keep query string

前端 未结 9 1735
栀梦
栀梦 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:20

    I don't think there is a way to define that in the routes configuration.

    Currently it is supported for routerLinks and imperative navigation to enable

    • preserveQueryParams and
    • preserveFragment

    You can add a guard to the empty path route, where in the guard navigation to the /comp1 route is done.

    router.navigate(['/comp1'], { preserveQueryParams: true }); //deprecated see update note
    
    router.navigate(['/comp1'], { queryParamsHandling: "merge" });
    

    There is a PR to allow to configure preserveQueryParams globally.

    Update note: from https://angular.io/api/router/NavigationExtras, preserveQueryParams is deprecated, use queryParamsHandling instead

提交回复
热议问题