移动到另一个组件时的角度句柄代码
我有 Angular 组件,它可以渲染一些数据并有按钮 Go to Dashboard,但我有两个选项可以移动到下一页。第一个如果用户在 10 秒后没有点击按钮 GoDashobard 应用程序会自动将他切换到该链接。
但是,如果用户仍然单击此按钮并转到下一个链接,则会在 10 秒后再次触发超时,并返回到该页面或刷新(如果已经在该页面上)。
我试图通过 isClickGoToDashboard 解决它,但它再次调用,可能是因为它在第一次之后加载到 ngOnInit () 上。
单击按钮不计算计数器并再次调用后,如何撤消此操作?
ngOnInit(): void {
if (!this.isClickGoToDashboard) {
setTimeout(() => {
this.router.navigateByUrl('home');
}, 10000);
}
}
goToDashboard() {
this.isClickGoToDashboard = true;
this.router.navigateByUrl('home');
}
回答
我认为你的问题的答案是 clearTimeout 并以这种方式使用它
delayAction;
ngAfterViewInit() {
this.delayAction = setTimeout(() =>
this.goToDashboard();
}, 10000);
}
goToDashboard() {
clearTimeout(this.delayAction);
this.router.navigateByUrl('home');
}