我有一个 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
从'@angular/core'导入{Component, HostListener};
然后绑定(bind)事件
@HostListener('window:popstate', ['$event'])
onBrowserBackBtnClose(event: Event) {
console.log('back button pressed');
event.preventDefault();
this.router.navigate(['/home'], {replaceUrl:true});
}
关于angular6 - 使用 Angular 6 中的浏览器后退按钮更改路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58185653/