How to handle query parameters in angular 2

后端 未结 12 771
灰色年华
灰色年华 2020-11-27 03:41

In my routable component I have

@RouteConfig {
  {path: \'/login\',   name: \'Login\', component: LoginComponent}
}  

But how

12条回答
  •  失恋的感觉
    2020-11-27 04:29

    Angular 5+ Update

    The route.snapshot provides the initial value of the route parameter map. You can access the parameters directly without subscribing or adding observable operators. It's much simpler to write and read:

    Quote from the Angular Docs

    To break it down for you, here is how to do it with the new router:

    this.router.navigate(['/login'], { queryParams: { token:'1234'} });
    

    And then in the login component (notice the new .snapshot added):

    constructor(private route: ActivatedRoute) {}
    ngOnInit() {
        this.sessionId = this.route.snapshot.queryParams['token']
    
    }
    

提交回复
热议问题