在Angular 2中有没有聪明的方法返回到最后一页?
类似的
this._router.navigate(LASTPAGE);
例如,C页有一个“返回”按钮,
A页-> C页,点击返回A页。 B页-> C页,点击它,回到B页。
路由器有这个历史信息吗?
在Angular 2中有没有聪明的方法返回到最后一页?
类似的
this._router.navigate(LASTPAGE);
例如,C页有一个“返回”按钮,
A页-> C页,点击返回A页。 B页-> C页,点击它,回到B页。
路由器有这个历史信息吗?
当前回答
在这些精彩的答案之后,我希望我的答案能找到一些人,并帮助他们。我编写了一个小服务来跟踪路线历史。开始了。
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;
}
}
其他回答
只需使用Location,这是一个Angular服务,应用程序可以使用它与浏览器的URL进行交互。
导入:
import { Location } from '@angular/common';
注射:
constructor(private location: Location) { }
简单地使用它:
goBack() {
this.location.back(); // Navigates back in the platform's history
}
在这些精彩的答案之后,我希望我的答案能找到一些人,并帮助他们。我编写了一个小服务来跟踪路线历史。开始了。
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;
}
}
在angular 4中使用preserveQueryParams,例如:
url: /list?page=1
<a [routerLink]="['edit',id]" [preserveQueryParams]="true"></a>
当点击链接时,您将被重定向编辑/10?Page =1,保留参数
裁判:https://angular.io/docs/ts/latest/guide/router.html # !# link-parameters-array
这是我想出来的,你也可以查一下是否有上一页。 确保在你的appComponent中使用了这个服务。
import { Injectable } from '@angular/core';
import { Location } from '@angular/common';
import { NavigationEnd, Router } from '@angular/router';
interface HistoryData {
previousPage: string | null,
currentPage: string | null,
}
@Injectable({ providedIn: 'root' })
export class GoBackService {
private historyData: HistoryData = { previousPage: null, currentPage: null };
constructor(private router: Router, private location: Location) {
this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {
this.historyData.previousPage = this.historyData.currentPage;
this.historyData.currentPage = event.urlAfterRedirects;
}
});
}
public goBack(): void {
if (this.historyData.previousPage) this.location.back();
}
public canGoBack(): boolean {
return Boolean(this.historyData.previousPage);
}
}
我是这么说的:
import { Location } from '@angular/common'
import { Component, Input } from '@angular/core'
@Component({
selector: 'Back_page',
template: `<button (click)="onBack()">Back</button>`,
})
export class BackPageComponent {
constructor(private location: Location) { }
onBack() {
this.location.back();// <-- go back to previous location
}
}