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

查看:22
本文介绍了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 类具有将 base href 考虑在内的 prepareExternalUrl().

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

Plunker 示例

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

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