Location

A service that applications can use to interact with a browser's URL.

一个服务,应用可以用它来与浏览器的 URL 互动。

class Location { static normalizeQueryParams(params: string): string static joinWithSlash(start: string, end: string): string static stripTrailingSlash(url: string): string path(includeHash: boolean = false): string isCurrentPathEqualTo(path: string, query: string = ''): boolean normalize(url: string): string prepareExternalUrl(url: string): string go(path: string, query: string = '', state: any = null): void replaceState(path: string, query: string = '', state: any = null): void forward(): void back(): void subscribe(onNext: (value: PopStateEvent) => void, onThrow?: ((exception: any) => void) | null, onReturn?: (() => void) | null): SubscriptionLike }

说明

Depending on which LocationStrategyis used, Location will either persist to the URL's path or the URL's hash segment.

这取决于使用了哪个 LocationStrategyLocation 可能会使用 URL 的路径进行持久化,也可能使用 URL 的哈希片段(#)进行持久化。

Note: it's better to use Routerservice to trigger route changes. Use Location only if you need to interact with or create normalized URLs outside of routing.

注意:最好使用 Router服务来触发路由变更。只有当你要在路由体系之外创建规范化 URL 或与之交互时才会用到 Location

Location is responsible for normalizing the URL against the application's base href. A normalized URL is absolute from the URL host, includes the application's base href, and has no trailing slash:

Location 负责基于应用的基地址(base href)对 URL 进行标准化。 所谓标准化的 URL 就是一个从主机(host)开始算的绝对地址,包括应用的基地址,但不包括结尾的斜杠:

  • /my/app/user/123 is normalized

    /my/app/user/123 是标准化的

  • my/app/user/123 is not normalized

    my/app/user/123 不是标准化的

  • /my/app/user/123/ is not normalized

    /my/app/user/123/ 不是标准化的

Example

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

静态方法

Given a string of url parameters, prepend with '?' if needed, otherwise return parameters as is.

给定 URL 参数字符串,如果需要则增加 '?' 前缀,否则原样返回。

static normalizeQueryParams(params: string): string

参数

params

Type: string.

返回值

string

Given 2 parts of a url, join them with a slash if needed.

给定 url 的两个部分,把它们连接(join)在一起,如有必要则添加一个斜杠。

static joinWithSlash(start: string, end: string): string

参数

start

Type: string.

end

Type: string.

返回值

string

If url has a trailing slash, remove it, otherwise return url as is. This method looks for the first occurrence of either #, ?, or the end of the line as / characters after any of these should not be replaced.

如果 url 具有结尾斜杠,则移除它,否则原样返回。 该方法会查找第一个 #? 之前的结尾 / 字符,之后的则不管。如果 url 中没有 #?,则替换行尾的。

static stripTrailingSlash(url: string): string

参数

url

Type: string.

返回值

string

方法

Returns the normalized URL path.

返回标准化之后的 URL 路径

path(includeHash: boolean = false): string

参数

includeHash

Type: boolean.

可选. 默认值是 false.

返回值

string

Normalizes the given path and compares to the current normalized path.

对指定的路径进行标准化,并和当前的标准化路径进行比较。

isCurrentPathEqualTo(path: string, query: string = ''): boolean

参数

path

Type: string.

query

Type: string.

可选. 默认值是 ''.

返回值

boolean

Given a string representing a URL, returns the normalized URL path without leading or trailing slashes.

给出一个字符串形式的 URL,返回一个标准化的 URL 路径,但不包括首尾的斜杠。

normalize(url: string): string

参数

url

Type: string.

返回值

string

Given a string representing a URL, returns the platform-specific external URL path. If the given URL doesn't begin with a leading slash ('/'), this method adds one before normalizing. This method will also add a hash if HashLocationStrategy is used, or the APP_BASE_HREF if the PathLocationStrategy is in use.

给出一个字符串形式的 URL,返回一个平台相关的外部 URL 路径。 如果鬼畜的 URL 不使用前导斜杠('/')开头的,那么该方法就会在标准化之前先添加一个。 如果正在使用 HashLocationStrategy 策略,则该方法还会添加一个 # 符号;如果正在使用 PathLocationStrategy 策略,则添加 APP_BASE_HREF

prepareExternalUrl(url: string): string

参数

url

Type: string.

返回值

string

Changes the browsers URL to the normalized version of the given URL, and pushes a new item onto the platform's history.

把浏览器的 URL 修改为指定 URL 的标准化版本,并往所属平台(如浏览器)的历史堆栈中追加一个新条目。

go(path: string, query: string = '', state: any = null): void

参数

path

Type: string.

query

Type: string.

可选. 默认值是 ''.

state

Type: any.

可选. 默认值是 null.

返回值

void

Changes the browsers URL to the normalized version of the given URL, and replaces the top item on the platform's history stack.

把浏览器的 URL 修改为指定 URL 的标准化版本,并替换所属平台(如浏览器)的历史堆栈的顶部条目。

replaceState(path: string, query: string = '', state: any = null): void

参数

path

Type: string.

query

Type: string.

可选. 默认值是 ''.

state

Type: any.

可选. 默认值是 null.

返回值

void

Navigates forward in the platform's history.

在所属平台(如浏览器)的历史堆栈中前进一步。

forward(): void

参数

没有参数。

返回值

void

Navigates back in the platform's history.

在所属平台(如浏览器)的历史堆栈中后退一步。

back(): void

参数

没有参数。

返回值

void

Subscribe to the platform's popState events.

订阅所属平台(如浏览器)的 popState 事件。

subscribe(onNext: (value: PopStateEvent) => void, onThrow?: ((exception: any) => void) | null, onReturn?: (() => void) | null): SubscriptionLike

参数

onNext

Type: (value: PopStateEvent) => void.

onThrow

Type: ((exception: any) => void) | null.

可选. 默认值是 undefined.

onReturn

Type: (() => void) | null.

可选. 默认值是 undefined.

返回值

SubscriptionLike

注解

@Injectable()