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路径
  • -
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、付费专栏及课程。

余额充值