4

I enter a link in url bar.

http://localhost:4200/reset-password-action/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9/5

I want to get eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 and 5 from url.

I have it in my app-routing.module.ts file.

const routes: Routes = [
  { path: 'reset-password-action/:token/:user_id', component: NewPasswordComponent }
];

I need a simple syntax to get this data.

3
  • 2
    Where do you want to get this data? Commented Sep 17, 2018 at 9:51
  • for now in the current component NewPasswordComponent Commented Sep 17, 2018 at 9:53
  • Have you read the documentation? angular.io/tutorial/toh-pt5#extract-the-id-route-parameter Commented Sep 17, 2018 at 9:53

3 Answers 3

9

First of all Where do you want to get this data? If you want it in the same component, in your case I think it is NewPasswordComponent

import {ActivatedRoute} from '@angular/router';
import {OnInit, Component} from '@angular/core';

@Component({...})
export class NewPasswordComponent implements OnInit {
  constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    console.log(this.route.snapshot.params.token);
    console.log(this.route.snapshot.params.user_id);
  } 
}

OR You can get like below also:

 constructor(private route: ActivatedRoute) {}

    ngOnInit() {
      this.route.params.subscribe(event => {
      this.token = event.token;
      this.user_id = event.user_id;
     });
    }
Sign up to request clarification or add additional context in comments.

Comments

2

You need to import activated route first then use this

import {Router, ActivatedRoute} from '@angular/router';
class ABC {
    id: any;
    token: any;
    constructor(private activatedRoute: ActivatedRoute) {}
    ngOnInit() {
        this.activatedRoute.params.subscribe(params => {
            if (typeof params['id'] !== 'undefined') {
                this.id = params['id'];
            } else {
                this.id = '';
            }
            if (typeof params['token'] !== 'undefined') {
                this.id = params['token'];
            } else {
                this.id = '';
            }
        });
    }
}

Comments

0

You can get query params like this using ActivatedRoute. Here is the example::

import {Router, ActivatedRoute, Params} from '@angular/router';
import {OnInit, Component} from '@angular/core';

@Component({...})
export class MyComponent implements OnInit {
 queryParamsStatus ='';

  constructor(private activatedRoute: ActivatedRoute) {}

  ngOnInit() {
    // subscribe to router event
    this.activatedRoute.params.subscribe((params: Params) => {
        this.queryParamsStatus= params['status'];
        console.log(queryParamsStatus );

      });
    let tempRows = this.rows.filter(s => s.staus === this.queryParamsStatus);
  }

}

You can get token/userId by simply replacing status by token or userid.

Hope this will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.