0

I would like to create a link for users to click on to verify their e-mail. To do that I would need to create the link. How do I convert a this.router to string? I would also need to retrieve the user's user id. If that is not possible, can I create a string that passes in the relative path?

I tried this:

let loginRoute = this.router.navigateByUrl("./login", {
      queryParams: { key: data.id }
});

but obviously it does not work since it .navigateByUrl returns a boolean result.

8
  • What have you done so far. Show me the code Commented May 16, 2019 at 3:41
  • @TonyNgo added the code Commented May 16, 2019 at 3:45
  • you can use ActivatedRoute.params then subscribe parameter from ur router. Commented May 16, 2019 at 3:45
  • @Nuttertools can you post the answer? Commented May 16, 2019 at 3:53
  • Answer in the below link might help solve your issue! Answer Commented May 16, 2019 at 4:04

2 Answers 2

1

Use DOCUMENT and Router to make the URL String with Params.

import DOCUMENT into a component from platform-browser and Router form @angular/router

import { DOCUMENT } from '@angular/platform-browser';

import { Router } from '@angular/router';

Initialize constructor

  constructor(
    private router: Router,
    @Inject(DOCUMENT) private document: any
  ) {}

make URL using

  ngOnInit() {
    let domain = this.document.location.hostname;
    this.href = this.router.url;
    console.log(domain+this.href)
  }

Working Sample - Stackblitz code is in the child-one component


------------ EDITED AFTER COMMENTS -------------

use DOCUMENT to get domain/hostname and concat parameters to that domain.

import { DOCUMENT } from '@angular/platform-browser';
import {ActivatedRoute} from '@angular/router';
constructor(
        @Inject(DOCUMENT) private document: any,
        private route:ActivatedRoute

      ) {}

ngOnInit() {
        let domain = this.document.location.hostname;
        let userId = this.route.snapshot.params['userId'];
        console.log(domain+'?user='+userId)
      }
Sign up to request clarification or add additional context in comments.

6 Comments

i dont think this solution works. this uses the current url. for my case, I would like to convert a relative path (not the current url) into a string, whilst adding the user's id at the back
i am doing this in my authentication service. after adding the data to database, i will get the id created as my parameters
can you post an example URL which you are trying to create? and also parameter getting via service
the example: localhost:4200/login?uid=<user's uid>. this is in the developer mode so when i go to the live website, it needs to change to website.com/login?uid=<user's id>. hence why i would like to string a relative path
so basically you just want a current domain name and you already know how to get other parameters like /login?uid=<user-id>
|
1
 constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
       this.id = +params['id'];
    });

This can get you the id in the query param.

If you want the domain, please do this -

const parsedUrl = new URL(window.location.href);
const baseUrl = parsedUrl.origin;
console.log(baseUrl);

2 Comments

what would be the datatypes for this.sub and this.id? i'm getting errors where i need to convert them to string
id: number; sub: Subscription

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.