Angular 2 访问基础 href [英] Angular 2 access base 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);
}
这篇关于Angular 2 访问基础 href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!