我有两个组件:客户组件和货运组件。当用户从货运组件跳转到客户组件时,我希望客户组件直接返回到货运组件。
我使用的方法是:从angular/common中的Location使用
这种方法总是直接返回到上一个页面。但是我只想在从货物组件返回时才直接返回。
我使用的方法是:从angular/common中的Location使用
this._location.back();
。这种方法总是直接返回到上一个页面。但是我只想在从货物组件返回时才直接返回。
this._location.back();
。更新针对 Angular 6
将以下代码插入到包含 2 个组件的父组件中:
import { Router, RoutesRecognized } from "@angular/router";
import { filter, pairwise } from 'rxjs/operators';
previousUrl:string;
constructor(private router: Router) {
router.events
.pipe(
filter(event => event instanceof RoutesRecognized),
pairwise()
)
.subscribe((e: any) => {
this.previousUrl = e[0].urlAfterRedirects;
});
}
它对我起作用了,希望对你也有效。
pairwise
,并将当前值和上一个值存储在服务中。 - Günter Zöchbauer