Reputation: 265
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.
Upvotes: 0
Views: 807
Reputation: 5526
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);
Upvotes: 1
Reputation: 2984
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)
}
Upvotes: 1