How to handle query parameters in angular 2

后端 未结 12 768
灰色年华
灰色年华 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:28

    (For Childs Route Only such as /hello-world)

    In the case you would like to make this kind of call :

    /hello-world?foo=bar&fruit=banana

    Angular2 doesn't use ? nor & but ; instead. So the correct URL should be :

    /hello-world;foo=bar;fruit=banana

    And to get those data :

    import { Router, ActivatedRoute, Params } from '@angular/router';
    
    private foo: string;
    private fruit: string;
    
    constructor(
      private route: ActivatedRoute,
      private router: Router
      ) {}
    
    ngOnInit() {
      this.route.params.forEach((params: Params) => {
          this.foo = params['foo'];
          this.fruit = params['fruit'];
      });
      console.log(this.foo, this.fruit); // you should get your parameters here
    }
    

    Source : https://angular.io/docs/ts/latest/guide/router.html

提交回复
热议问题