填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

PathLocationStrategy

LocationStrategy用來配置 Location服務,以便在瀏覽器 URL 的 path 中表示其狀態。

A LocationStrategyused to configure the Locationservice to represent its state in the path of the browser's URL.

檢視"說明"...

      
      class PathLocationStrategy extends LocationStrategy implements OnDestroy {
  ngOnDestroy(): void
  onPopState(fn: LocationChangeListener): void
  getBaseHref(): string
  prepareExternalUrl(internal: string): string
  path(includeHash: boolean = false): string
  pushState(state: any, title: string, url: string, queryParams: string)
  replaceState(state: any, title: string, url: string, queryParams: string)
  forward(): void
  back(): void
  historyGo(relativePosition: number = 0): void

  // 繼承自 common/LocationStrategy
  abstract path(includeHash?: boolean): string
  abstract prepareExternalUrl(internal: string): string
  abstract pushState(state: any, title: string, url: string, queryParams: string): void
  abstract replaceState(state: any, title: string, url: string, queryParams: string): void
  abstract forward(): void
  abstract back(): void
  historyGo(relativePosition: number)?: void
  abstract onPopState(fn: LocationChangeListener): void
  abstract getBaseHref(): string
}
    

說明

如果你使用 PathLocationStrategy ,則必須提供一個 APP_BASE_HREF或在文件中新增 <base href>

If you're using PathLocationStrategy, you must provide a APP_BASE_HREFor add a <base href> element to the document.

例如,如果你提供的 APP_BASE_HREF'/my/app/',並呼叫 location.go('/foo'),則瀏覽器的 URL 將變為 example.com/my/app/foo。為了確保所有相對 URI 都能正確解析,<base href> 和/或 APP_BASE_HREF 都應該以 / 結尾。

For instance, if you provide an APP_BASE_HREF of '/my/app/' and call location.go('/foo'), the browser's URL will become example.com/my/app/foo. To ensure all relative URIs resolve correctly, the <base href> and/or APP_BASE_HREF should end with a /.

同樣,如果將 <base href='/my/app/'/> 新增到文件中並呼叫 location.go('/foo') ,則瀏覽器的 URL 將變為 example.com/my/app/foo

Similarly, if you add <base href='/my/app/'/> to the document and call location.go('/foo'), the browser's URL will become example.com/my/app/foo.

請注意,使用 PathLocationStrategy 時,如 RFC 所述,查詢和 <base href> 的片段部分都不會保留。

Note that when using PathLocationStrategy, neither the query nor the fragment in the <base href> will be preserved, as outlined by the RFC.

Further information available in the Usage Notes...

方法

      
      ngOnDestroy(): void
    
引數

沒有引數。

返回值

void

      
      onPopState(fn: LocationChangeListener): void
    
引數
fn LocationChangeListener
返回值

void

      
      getBaseHref(): string
    
引數

沒有引數。

返回值

string

      
      prepareExternalUrl(internal: string): string
    
引數
internal string
返回值

string

      
      path(includeHash: boolean = false): string
    
引數
includeHash boolean
可選. 預設值是 `false`.
返回值

string

      
      pushState(state: any, title: string, url: string, queryParams: string)
    
引數
state any
title string
url string
queryParams string
      
      replaceState(state: any, title: string, url: string, queryParams: string)
    
引數
state any
title string
url string
queryParams string
      
      forward(): void
    
引數

沒有引數。

返回值

void

      
      back(): void
    
引數

沒有引數。

返回值

void

      
      historyGo(relativePosition: number = 0): void
    
引數
relativePosition number
可選. 預設值是 `0`.
返回值

void

使用說明

例子

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