showing results for - "typescript read url parameters"
Fynn
11 May 2020
1// get all search params (including ?)
2const queryString = window.location.search;
3// it will look like this: ?product=shirt&color=blue&newuser&size=m
4
5// parse the query string's paramters
6const urlParams = new URLSearchParams(queryString);
7
8// To get a parameter simply write something like the follwing
9const paramValue = urlParams.get('yourParam');
Mckenna
31 Nov 2018
1import {Router, ActivatedRoute, Params} from '@angular/router';
2import {OnInit, Component} from '@angular/core';
3
4@Component({...})
5export class MyComponent implements OnInit {
6
7  constructor(private activatedRoute: ActivatedRoute) {}
8
9  ngOnInit() {
10    // Note: Below 'queryParams' can be replaced with 'params' depending on your requirements
11    this.activatedRoute.queryParams.subscribe(params => {
12        const userId = params['userId'];
13        console.log(userId);
14      });
15  }
16
17}
Fabiana
22 Jun 2017
1const product = urlParams.get('product')
2console.log(product);
3// shirt
4
5const color = urlParams.get('color')
6console.log(color);
7// blue
8
9const newUser = urlParams.get('newuser')
10console.log(newUser);
11// empty string
12