Angular 2访问基础href [英] Angular 2 access base href

查看:141
本文介绍了Angular 2访问基础href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案中有一个代码,可以获取应用程序的baseHref

I have a code in my solution which gets baseHref of an app

constructor( private router: Router) {
    let baseHref = this.router._location._baseHref;

您可以看到我通过私有财产得到的东西.还有其他获取基本网址的方法吗?又是路由器添加它的一部分之前的网址?

You could see what I am getting it via private properties. Is there other way to get base url? aka the url before router added it's parts?

我正在使用angular2 rc4

I am using angular2 rc4

谢谢

推荐答案

Location类具有prepareExternalUrl(),它考虑了基本href.

The Location class has prepareExternalUrl() that takes base href into account.

您也可以尝试注入constructor(@Inject(APP_BASE_HREF) href:string)

似乎APP_BASE_HREF如果没有明确提供,将无法再注入.

It seems APP_BASE_HREF can't be injected anymore without explicitly providing it.

PlatformLocation 提供有关URL的更多详细信息:

PlatformLocation provides more details about the URL:

  import {PlatformLocation } from '@angular/common';

  constructor(platformLocation: PlatformLocation) {
    console.log((platformLocation as any).location);
    console.log((platformLocation as any).location.href);
    console.log((platformLocation as any).location.origin);
  }

> 柱塞示例

这篇关于Angular 2访问基础href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆