填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

Location

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

A service that applications can use to interact with a browser's 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
  getState(): unknown
  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
  historyGo(relativePosition: number = 0): void
  onUrlChange(fn: (url: string, state: unknown) => void)
  subscribe(onNext: (value: PopStateEvent) => void, onThrow?: (exception: any) => void, onReturn?: () => void): SubscriptionLike
}
    

Provided in

说明

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

Depending on the LocationStrategy used, Location persists to the URL's path or the URL's hash segment.

Further information available in the Usage Notes...

静态属性

属性说明
static normalizeQueryParams: (params: string) => string

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

Normalizes URL parameters by prepending with ? if needed.

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

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

Joins two parts of a URL with a slash if needed.

static stripTrailingSlash: (url: string) => string

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

Removes a trailing slash from a URL string if needed. Looks for the first occurrence of either #, ?, or the end of the line as / characters and removes the trailing slash if one exists.

方法

返回标准化之后的 URL 路径

Normalizes the URL path for this location.

      
      path(includeHash: boolean = false): string
    
参数
includeHash boolean

路径中是否包含一个锚点片段(Anchor fragment)。

True to include an anchor fragment in the path.

可选. 默认值是 `false`.
返回值

标准化之后的 URL 路径。

string: The normalized URL path.

报告位置历史记录的当前状态。

Reports the current state of the location history.

      
      getState(): unknown
    
参数

没有参数。

返回值

history.state 对象的当前值。

unknown: The current value of the history.state object.

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

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

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

指定的 URL 路径。

The given URL path.

query string

查询参数。

Query parameters.

可选. 默认值是 `''`.
返回值

如果指定的 URL 路径和标准化之后的路径一样,则返回 true,否则返回 false

boolean: True if the given URL path is equal to the current normalized path, false otherwise.

给出一个字符串形式的 URL,返回一个去掉末尾斜杠之后的 URL 路径。

Normalizes a URL path by stripping any trailing slashes.

      
      normalize(url: string): string
    
参数
url string

表示一个 URL。

String representing a URL.

返回值

标准化之后的 URL 字符串。

string: The normalized URL string.

标准化外部 URL 路径。如果给定的 URL 并非以斜杠( '/' )开头,就会在规范化之前添加一个。如果使用 HashLocationStrategy 则添加哈希;如果使用 PathLocationStrategy 则添加 APP_BASE_HREF

Normalizes an external URL path. If the given URL doesn't begin with a leading slash ('/'), adds one before normalizing. Adds a hash if HashLocationStrategy is in use, or the APP_BASE_HREF if the PathLocationStrategy is in use.

      
      prepareExternalUrl(url: string): string
    
参数
url string

表示一个 URL。

String representing a URL.

返回值

标准化之后的平台相关 URL。

string: A normalized platform-specific URL.

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

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

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

要标准化的路径。

URL path to normalize.

query string

查询参数。

Query parameters.

可选. 默认值是 `''`.
state any

历史状态。

Location history state.

可选. 默认值是 `null`.
返回值

void

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

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

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

要标准化的路径

URL path to normalize.

query string

查询参数

Query parameters.

可选. 默认值是 `''`.
state any

历史状态

Location history state.

可选. 默认值是 `null`.
返回值

void

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

Navigates forward in the platform's history.

      
      forward(): void
    
参数

没有参数。

返回值

void

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

Navigates back in the platform's history.

      
      back(): void
    
参数

没有参数。

返回值

void

Navigate to a specific page from session history, identified by its relative position to the current page.

See also:

      
      historyGo(relativePosition: number = 0): void
    
参数
relativePosition number

Position of the target page in the history relative to the current page. A negative value moves backwards, a positive value moves forwards, e.g. location.historyGo(2) moves forward two pages and location.historyGo(-2) moves back two pages. When we try to go beyond what's stored in the history session, we stay in the current page. Same behaviour occurs when relativePosition equals 0.

可选. 默认值是 `0`.
返回值

void

注册 URL 更改监听器。被 Angular 框架用于捕获那些无法通过 “popstate” 或 “hashchange” 事件检测到的更新。

Registers a URL change listener. Use to catch updates performed by the Angular framework that are not detectible through "popstate" or "hashchange" events.

      
      onUrlChange(fn: (url: string, state: unknown) => void)
    
参数
fn (url: string, state: unknown) => void

更改处理器函数,接受 URL 和位置历史记录的状态。

The change handler function, which take a URL and a location history state.

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

Subscribes to the platform's popState events.

      
      subscribe(onNext: (value: PopStateEvent) => void, onThrow?: (exception: any) => void, onReturn?: () => void): SubscriptionLike
    
参数
onNext (value: PopStateEvent) => void
onThrow (exception: any) => void
可选. 默认值是 `undefined`.
onReturn () => void
可选. 默认值是 `undefined`.
返回值

已订阅的事件。

SubscriptionLike: Subscribed events.

使用说明

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

It's better to use the Router#navigate service to trigger route changes. Use Location only if you need to interact with or create normalized URLs outside of routing.

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

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:

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

    /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

例子

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