Angular2 router keep query string

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

    After having had a go at most answers, I found that

    • Günter Zöchbauer's answer doesn't work for me at all
    • Christopher's suggestion of removing the leading / didn't do it either
    • AArias' answer did work but lead to the adding of two urls in the history:
      1. https://my.application.com/comp1?param=val <= ( ಠ 益ಠ )
      2. https://my.application.com/comp1;param=val

    So here's yet another approach, that eventually behaved as per my expectations:

    import { ActivatedRoute, Router } from '@angular/router';
    
    class Component {
        constructor(private route: ActivatedRoute, private router: Router) {}
    
        someMethod() {
            router.navigate(['/comp1', this.route.snapshot.params]);
        }
    }
    

提交回复
热议问题