我不会在退出/重新加载页面上保存用户数据:
@HostListener('window:beforeunload', ['$event']) async unloadHandler() {
await this.api.saveUserSettings(this.userSetting);
}
这有时是工作,有时不是。 在Chrome网络中,我看到请求始终被取消。
答案 0 :(得分:0)
因为您无法通过非阻止操作阻止用户离开页面。
相反,您应该将设置保存在本地存储中
@HostListener('window:beforeunload', ['$event']) async unloadHandler() {
localStorage.setItem('settings', this.userSetting);
}
并使用resolver或application initializer发出请求,在重新加载页面后。