Location

功能: Location是一种服务,应用通过浏览器的URL可以使用这个服务进行交互。

类描述 Location的使用是基于LocationStrategy的使用,Location既可以用于URL的路径也可以用于URL哈希片段。

注意:这比用Router服务触发路由改变要好的多。使用Location服务你只需在路由的外部创建一个正常的URL即可交互。

Location负责在应用基础的href上正常化一个URL,一个正常化的URL来自于URL的host,这自然包含了应用的基础href.而且注意它的结尾处是不带反斜杠的。

import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
import {Component} from '@angular/core';
@Component({
  selector: 'path-location',
  providers: [Location, {provide: LocationStrategy, useClass: PathLocationStrategy}],
  template: `
    <h1>PathLocationStrategy</h1>
    Current URL is: <code>{{location.path()}}</code><br>
    Normalize: <code>/foo/bar/</code> is: <code>{{location.normalize('foo/bar')}}</code><br>
  `
})
export class PathLocationComponent {
  location: Location;
  constructor(location: Location) { this.location = location; }
}

注释: @Injectable();
构造函数:constructor(platformStrategy: LocationStrategy)
静态成员:

  • normalizeQueryParams(params: string) : string
  • 给定一个URL字符串参数,如果需要的话还可以添加?
  • joinWithSlash(start: string, end: string) : string
  • 合并url的两个部分,通过反斜杠合并。
  • stripTrailingSlash(url: string) : string
  • 如果url有反斜杠,就删除。

类详情:

  • path(includeHash?: boolean) : string 返回正常化的URL路径
  • isCurrentPathEqualTo(path: string, query?: string) : boolean 正常化赋值的路径并同当前正常化的路径进行对比
  • normalize(url: string) : string 赋值一个字符串表示一个URL, 返回一个不带反斜杠的正常化URL路径
  • prepareExternalUrl(url: string) : string 赋值一个字符串表示URL,返回平台定义的额外URL路径。如果赋值的URL并不是通过反斜杠开始的话,这个方法就会在正常化之前添加一个反斜杠符号。如果HashLocationStrategy类正在使用的话,这个方法也会添加一个哈希,或者添加一个APP_BASH_HREF,在使用PathLocationStrategy类的情况下。
  • go(path: string, query?: string) : void 改变浏览器的URL到已经赋值的URL上,并且刷新平台历史列表。
  • replaceState(path: string, query?: string) : void 跳转浏览器的URL新赋值的URL上,代替平台历史栈的的第一条。
  • forward():void 平台历史的前进
  • back():void 平台历史后退
  • subscribe(onNext: (value: any) => void, onThrow?: (exception: any) => void, onReturn?: () => void) : Object 给平台popState事件背书签字。
class Location {

staticnormalizeQueryParams(params: string) : string

staticjoinWithSlash(start: string, end: string) : string

staticstripTrailingSlash(url: string) : string

constructor(platformStrategy: LocationStrategy)

path(includeHash?: boolean) : string

isCurrentPathEqualTo(path: string, query?: string) : boolean

normalize(url: string) : string

prepareExternalUrl(url: string) : string

go(path: string, query?: string) : void

replaceState(path: string, query?: string) : void

forward() : void

back() : void

subscribe(onNext: (value: any) => void, onThrow?: (exception: any) => void, onReturn?: () => void) : Object

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值