填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫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;
  }
}