使用角度为6的浏览器后退按钮更改路线

问题描述 投票:0回答:1

我在angular6中有一个网站,我想使用onPopState单击浏览器后退按钮来更改URL,但它不起作用。所需路线组件的TS文件已加载,但无法保留,并且无法根据浏览器历史记录进行重定向。

我正在使用下面的代码

constructor(private fb: FormBuilder, 
    private _phonenumberService: PhonenumberService, 
    private router: Router, 
    private apiService: ApiServiceService, 
    public location: Location, 
    public plocation: LocationStrategy) {
            this.plocation.onPopState(() => {
              console.log('Back button pressed');
              this.router.navigate(['/home'],  {replaceUrl:true});
          });

提前感谢。

angular6 back-button
1个回答
0
投票

尝试这个。导入HostListener

import { Component, HostListener } from '@angular/core';

然后绑定事件

@HostListener('window:popstate', ['$event'])
onBrowserBackBtnClose(event: Event) {
    console.log('back button pressed');
    event.preventDefault(); 
    this.router.navigate(['/home'],  {replaceUrl:true});
}
© www.soinside.com 2019 - 2024. All rights reserved.