【问题标题】:Angular : how to get url on page load and check if contain textAngular:如何在页面加载时获取 url 并检查是否包含文本
【发布时间】:2021-10-05 02:03:57
【问题描述】:

我能够使用路由paramMap 获得authkey 值,但我们如何获得网址。例如,我想检查 URL 是否包含 resetpassword 文本,如下面的链接所示。我们如何在 Angular 中做到这一点?

我们如何获取 URL 或者 URL 是否在页面加载时包含某些文本?检查 URL 是否包含“resetpassword”文本。

代码

ngOnInit(): void {
  this.router.paramMap.subscribe((paramMap) => {
    console.log("paramMap" , paramMap)
    this.code = paramMap.get('authkey');
    if (this.code) {
      this.code = paramMap.get('authkey').replace('authkey:', '');
      this.getEmail(this.code);
    }
  });
}

示例网址

http://localhost:4200/#/login/resetpassword/authkey:6762b362-1a62-4077-8ae4-78b381REX7JEXE4a8f30

【问题讨论】:

  • 这能回答你的问题吗? Get current url in Angular
  • 不,因为我还想检查 url 是否包含特定字符串
  • 真的找不到解决办法
  • 好的。 2 种方法:您可以按照其他答案中的说明获取 url,然后在 google 中搜索“js 检查字符串是否包含子字符串”-> stackoverflow.com/questions/1789945/…。或者你直接谷歌“js检查url是否包含文本”->stackoverflow.com/questions/4597050/…
  • let subscring ="resetpassword" console.log("thisss" ,this.router.url.includes(subscring))

标签: angular typescript


【解决方案1】:

include 是一个函数,可以用来在字符串中查找字符串值

if(this.router.url.includes('resetpassword')){
  //Implement code here if exists 
}else{
 //Implement code here if not exists 
}

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 2019-07-14
    • 2012-10-11
    • 2016-03-05
    相关资源
    最近更新 更多