如何获取angular中的上一页URL 5
How to get the previous page URL in angular 5
我正在使用 Angular 5 应用程序,我需要知道如何获取最后一个 URL 以将其作为 link 放置到我的后退按钮上。我找到了这个 location.back()
但我需要最后一个 url 作为字符串。我怎样才能得到生成 location.back()
?
的字符串
非常感谢!
在浏览器环境中,location.back()
是 window.history
对象的包装器(有关通过源的部分路径,请参阅 here, here, here and here)。
History 对象中的内容是故意不可访问的。
来自HTML History interface documention:
The actual entries are not accessible from script.
来自MDN the window.history object documentation:
For security reasons the History object doesn't allow the
non-privileged code to access the URLs of other pages in the session
history, but it does allow it to navigate the session history.
关于替代方法,this answer provides a technique for listening for the last 2 NavigationEnd events。
Angular 6 更新代码以获取先前的 url 作为字符串。
import { Component, OnInit } from '@angular/core';
import { Router, RoutesRecognized } from '@angular/router';
import { filter, pairwise } from 'rxjs/operators';
export class AppComponent implements OnInit {
constructor (
public router: Router
) {
}
ngOnInit() {
this.router.events
.pipe(filter((e: any) => e instanceof RoutesRecognized),
pairwise()
).subscribe((e: any) => {
console.log(e[0].urlAfterRedirects); // previous url
});
}
我正在使用 Angular 5 应用程序,我需要知道如何获取最后一个 URL 以将其作为 link 放置到我的后退按钮上。我找到了这个 location.back()
但我需要最后一个 url 作为字符串。我怎样才能得到生成 location.back()
?
非常感谢!
在浏览器环境中,location.back()
是 window.history
对象的包装器(有关通过源的部分路径,请参阅 here, here, here and here)。
History 对象中的内容是故意不可访问的。
来自HTML History interface documention:
The actual entries are not accessible from script.
来自MDN the window.history object documentation:
For security reasons the History object doesn't allow the non-privileged code to access the URLs of other pages in the session history, but it does allow it to navigate the session history.
关于替代方法,this answer provides a technique for listening for the last 2 NavigationEnd events。
Angular 6 更新代码以获取先前的 url 作为字符串。
import { Component, OnInit } from '@angular/core';
import { Router, RoutesRecognized } from '@angular/router';
import { filter, pairwise } from 'rxjs/operators';
export class AppComponent implements OnInit {
constructor (
public router: Router
) {
}
ngOnInit() {
this.router.events
.pipe(filter((e: any) => e instanceof RoutesRecognized),
pairwise()
).subscribe((e: any) => {
console.log(e[0].urlAfterRedirects); // previous url
});
}