我在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});
});
提前感谢。
尝试这个。导入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});
}