我有一个使用版本 9 的 Angular 应用程序。从我的一条路线(例如/search/details/id),我使用从 API 响应收到的 URL 值重定向到外部 URL,例如
window.location.href = "https://www.payment.com";
Run Code Online (Sandbox Code Playgroud)
现在我被重定向到外部付款页面,我可以在其中付款或取消交易。
如果我单击“取消”,我将被重定向回上一页,这是我的角度应用程序,路线为/search/details/:id。从这里,如果我单击浏览器后退按钮,我将进入支付页面https://www. payment.com。我想导航到 Angular app /search路线,而不是我最初导航到 /search/details/id 的路线。
我尝试通过以下方式处理组件中的浏览器后退按钮,
1. fromEvent(window, 'popstate')
.subscribe((e) => {
console.log(e, 'back button');
this.router.navigate(
['/search']);
});
Added inside the constructor of the component corresponding to /search/details/:id
2. router.events.forEach((event) => {
if(event instanceof NavigationStart) {
if (event.navigationTrigger === 'popstate') {
console.log(event, 'back button router');
}
}
});
Added inside constructor
3. @HostListener('window:popstate', ['$event'])
onPopState(event) {
this.router.navigate( …Run Code Online (Sandbox Code Playgroud)