简体   繁体   中英

How do I string a relative path

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.

 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);

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)
      }

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