简体   繁体   English

我如何字符串相对路径

[英]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? 如何将this.router转换为字符串? I would also need to retrieve the user's user id. 我还需要检索用户的用户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. 但显然它不起作用,因为.navigateByUrl返回一个布尔结果。

 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. 这样可以获取查询参数中的ID。

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. 使用DOCUMENTRouter来使URL字符串带有Params。

import DOCUMENT into a component from platform-browser and Router form @angular/router platform-browser和路由器格式@ 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 工作示例-Stackblitz代码位于子组件一中


------------ EDITED AFTER COMMENTS ------------- ------------编辑后的评论-------------

use DOCUMENT to get domain/hostname and concat parameters to that domain. 使用DOCUMENT获取该域的域名/主机名和concat参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM