回答:
実際には、「Back」APIを所有する組み込みのLocationサービスを利用できます。
ここに(TypeScriptで):
import {Component} from '@angular/core';
import {Location} from '@angular/common';
@Component({
  // component's declarations here
})
class SomeComponent {
  constructor(private _location: Location) 
  {}
  backClicked() {
    this._location.back();
  }
}
編集:@ charith.arumapperumaで述べられLocationている@angular/commonようにからインポートする必要があるため、import {Location} from '@angular/common';行は重要です。
Location次のように述べています。@Sasxaの答えは明らかにRouterこれを行う方法を示しています。ただし、このLocation方法の方が明らかに便利です。Routerメソッドがメソッドよりも正しいかもしれない理由を誰かが知っていLocationますか?
                    Angular 2.x / 4.x の最終バージョン -ここにドキュメントがありますhttps://angular.io/api/common/Location
/* typescript */
import { Location } from '@angular/common';
// import stuff here
@Component({
// declare component here
})
export class MyComponent {
  // inject location into component constructor
  constructor(private location: Location) { }
  cancel() {
    this.location.back(); // <-- go back to previous location on cancel
  }
}
              <button backButton>BACK</button>これをディレクティブに入れて、クリック可能な要素に添付できます。
import { Directive, HostListener } from '@angular/core';
import { Location } from '@angular/common';
@Directive({
    selector: '[backButton]'
})
export class BackButtonDirective {
    constructor(private location: Location) { }
    @HostListener('click')
    onClick() {
        this.location.back();
    }
}
使用法:
<button backButton>BACK</button>
              あなたが代わりにボタンのアンカーを使用する場合、あなたはそれにする必要があり、受動的リンクとのhref="javascript:void(0)"角度位置を動作させるために。
app.component.ts
import { Component } from '@angular/core';
import { Location } from '@angular/common';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent {
  constructor( private location: Location ) { 
  }
  goBack() {
    // window.history.back();
    this.location.back();
    console.log( 'goBack()...' );
  }
}
app.component.html
<!-- anchor must be a passive link -->
<a href="javascript:void(0)" (click)="goBack()">
  <-Back
</a>
              javascript:void(0)ます。何か…  @Directive({ selector: '[clickPreventDefault]' })  export class ClickPreventDefaultDirective { 	@HostListener("click", ["$event"]) onClick($event: Event) { 		$event.preventDefault(); 	}  }
                    あなたのrouterOnActivate()ルートクラスにメソッドを実装することができます、それは以前のルートについての情報を提供します。
routerOnActivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction) : any
その後router.navigateByUrl()、から生成されComponentInstructionたデータを使用して渡すことができます。例えば:
this._router.navigateByUrl(prevInstruction.urlPath);
              routerOnActivate
                    また、ファイルシステムのように戻る必要がある場合にも機能します。 PS @角度:「^ 5.0.0」
<button type="button" class="btn btn-primary" routerLink="../">Back</button>
              アプリのどこでも再利用できるボタンを作成しました。
このコンポーネントを作成する
import { Location } from '@angular/common';
import { Component, Input } from '@angular/core';
@Component({
    selector: 'back-button',
    template: `<button mat-button (click)="goBack()" [color]="color">Back</button>`,
})
export class BackButtonComponent {
    @Input()color: string;
  constructor(private location: Location) { }
  goBack() {
    this.location.back();
  }
}
次に、戻るボタンが必要なときにテンプレートに追加します。
<back-button color="primary"></back-button>
注:これはAngular Materialを使用しています。そのライブラリを使用していない場合は、mat-buttonおよびを削除してくださいcolor。
これらすべての素晴らしい答えの後に、私の答えが誰かを見つけて助けてくれることを願っています。ルート履歴を追跡するための小さなサービスを書きました。いきます
import { Injectable } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { filter } from 'rxjs/operators';
@Injectable()
export class RouteInterceptorService {
  private _previousUrl: string;
  private _currentUrl: string;
  private _routeHistory: string[];
  constructor(router: Router) {
    this._routeHistory = [];
    router.events
      .pipe(filter(event => event instanceof NavigationEnd))
      .subscribe((event: NavigationEnd) => {
        this._setURLs(event);
      });
  }
  private _setURLs(event: NavigationEnd): void {
    const tempUrl = this._currentUrl;
    this._previousUrl = tempUrl;
    this._currentUrl = event.urlAfterRedirects;
    this._routeHistory.push(event.urlAfterRedirects);
  }
  get previousUrl(): string {
    return this._previousUrl;
  }
  get currentUrl(): string {
    return this._currentUrl;
  }
  get routeHistory(): string[] {
    return this._routeHistory;
  }
}
              別のページに移動するときに行った方法は、現在の場所を渡すことでクエリパラメータを追加します
this.router.navigate(["user/edit"], { queryParams: { returnUrl: this.router.url }
コンポーネントでこのクエリパラメータを読み取ります
this.router.queryParams.subscribe((params) => {
    this.returnUrl = params.returnUrl;
});
returnUrlが存在する場合は、戻るボタンを有効にし、ユーザーが戻るボタンをクリックしたときに
this.router.navigateByUrl(this.returnUrl); // Hint taken from Sasxa
これで前のページに移動できます。location.backを使用する代わりに、上記の方法の方がより安全だと思います。ユーザーが直接ページに到達し、location.backで戻るボタンを押すと、ユーザーをWebページではない前のページにリダイレクトします。
角度4での使用preserveQueryParams、例:
url: /list?page=1
<a [routerLink]="['edit',id]" [preserveQueryParams]="true"></a>
リンクをクリックすると、リダイレクトされedit/10?page=1、パラメータが保持されます
ref:https : //angular.io/docs/ts/latest/guide/router.html#! #link-parameters- array