加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在Angular 2.0中获取当前URL

发布时间:2020-12-17 17:33:41 所属栏目:安全 来源:网络整理
导读:我知道这已被问过很多次了,我花了2个多小时的时间寻找解决方案. 所以请不要评论重复或投票反复询问重复的问题. 我当前的URL是:http:// localhost:4200 / configuration 以下是我尝试实施的许多在线找到的解决方案之一. export class AppComponent impleme
我知道这已被问过很多次了,我花了2个多小时的时间寻找解决方案.

所以请不要评论重复或投票反复询问重复的问题.

我当前的URL是:http:// localhost:4200 / configuration

以下是我尝试实施的许多在线找到的解决方案之一.

export class AppComponent implements OnInit{
  title = 'app';
  constructor(private router: Router,private activatedRoute: ActivatedRoute) { 

  }

  ngOnInit() { 
    console.log("On Init");
    console.log(this.router.url);
    console.log(this.activatedRoute.url);
    //this.router.navigate(['/login']);
  }
}

在重新加载页面时,我得到低于输出.

On Init 

我只是空洞的网址.
我很想知道自己错过了什么.

谢谢你的帮助.

解决方法

您可以使用location.href来完成.另一种选择是使用’@ angular / platform-b??rowser’中的DOCUMENT:

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

constructor(@Inject(DOCUMENT) private document: any){
    console.log(location.pathname);
    console.log(location.href);
    console.log(this.document.location.href);
}

如果您只想从网址获取“/ configuration”,请使用location.pathname.

演示here.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读