简体   繁体   中英

Using QueryParams in Angular route

So I have a Back-End that's in charge of sending confirmation emails to users when they create an account, the email has a format like: http://localhost:8080/confirm?token=

I then want to have route with the same format on my Front-End side (which is made with Angular), so my question is how this url should be represented in my application routes. I'm currently trying to use { path: 'confirm', paramMap: { token : token } } but not sure this is the best way to handle this, this is my first time doing something like this so I'd appreciate any input :)

I later need to use that token value in a service so I need a way to obtain its value in the "confirm" component or something.

I have an example of using query parameters here: https://github.com/DeborahK/MovieHunter-routing

As Jon stated, you don't specify query parameters in the route configuration:

在此处输入图片说明

UPDATE: Updated the screen shot to correct a typo and for latest syntax.

Inject the ActivatedRoute in your Component constructor and then access params using ActivatedRoute instance.

    export class TestComponent Implements OnInit
    {
     token:string;
    constructor(private route:ActivatedRoute)
    {
    }
   ngOnInit()
   {
    route.params.subscribe((params:Params)=>{
      this.token=params['token'];
      //Or call your service to load the resource you need.
   })

   }
}

This is just sample example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM