Kas*_*ner 9 back-button-control angular angular-material-stepper
我的Angular应用程序中有一个页面,它使用stepper. 例如,当用户处于第 3 步时,他要返回到上一步(第 2 步)要做的第一个合乎逻辑的事情是单击back button。但显然,他会被重定向到上一页。
因此,我可以在stepper后退单击时将其设置为上一页,而不是将用户重定向到最后一页,并且当用户处于步骤 1时将他重定向到最后一页。
我试图@HostListener通过强制转换this.stepper.previous();(将步进器设置为上一个的函数)来使用,但是当操作是时,catched该函数不会执行,并且页面无论如何都会重定向回来。
那么如何防止页面在单击后退按钮时重定向?
这是我尝试过的:
@HostListener( 'window: popstate', ['$event'])
onPopState(event: Event): void {
event.preventDefault();
console.log("BACK PRESSED")
this.stepper.previous();
}
Run Code Online (Sandbox Code Playgroud)
你的问题有两点:
disable back整个应用程序或多个组件的按钮disable back特定组件的按钮对于#1 要求,我认为更好的方法是实现 aGuard并将其应用到所需的routes. 但是,如果需要#2,使用onPopState()看起来不错,但似乎您错过了使用LocationStrategy and history.pushState(),所以试试这个:
import { LocationStrategy } from '@angular/common';
@Component({
selector: 'app-root'
})
export class AppComponent {
constructor(
private location: LocationStrategy
) {
history.pushState(null, null, window.location.href);
// check if back or forward button is pressed.
this.location.onPopState(() => {
history.pushState(null, null, window.location.href);
this.stepper.previous();
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21009 次 |
| 最近记录: |