[英]How to implement Service Worker in Angular for app update notifications
I have installed the angular service worker module and applied the appropriate code to the app module.我已经安装了角度服务工作者模块并将适当的代码应用于应用程序模块。 I have an updateComponent
that has the UI to notify the user that there's a new version available with an update button that will reload the page.我有一个updateComponent
,它的 UI 可以通知用户有一个可用的新版本,其中有一个更新按钮,它将重新加载页面。
However, in a previous version of the service worker, the code to make the notification UI appear and become removed on button click (reload), was the following:但是,在之前版本的 service worker 中,使通知 UI 出现并在按钮单击(重新加载)时被删除的代码如下:
export class LogUpdateService {
updateAvailable: boolean = false;
constructor(updates: SwUpdate) {
updates.available.subscribe(event => {
// console.log('current version is', event.current);
// console.log('available version is', event.available);
this.updateAvailable = true;
});
updates.activated.subscribe(event => {
// console.log('old version was', event.previous);
// console.log('new version is', event.current);
});
}
}
In the latest version, available
has been depreciated.在最新版本中, available
已折旧。
I have added the following which achieves the UI component notification appearing.我添加了以下实现 UI 组件通知出现。 When the update
button in the notification component is clicked, the page is reloaded (as intended).单击通知组件中的update
按钮时,将重新加载页面(如预期的那样)。 However, the notification component is still present and doesn't disappear.但是,通知组件仍然存在并且不会消失。 This component is governed by an ngIf
with the updateAvailable
boolean from the service below.该组件由来自以下服务的带有ngIf
布尔值的updateAvailable
。
export class AppUpdateService {
updateAvailable: boolean = false;
constructor(
private swUpdate: SwUpdate
) {
swUpdate.checkForUpdate().then(() => {
this.updateAvailable = true;
});
}
}
Question问题
Once the update
button in the notification component has been clicked and the page has reloaded, why is updateAvailable
still true?一旦点击了通知组件中的update
按钮并重新加载了页面,为什么updateAvailable
仍然为真? Is there another condition in the latest version of the service worker that checks if the update has been applied?最新版本的 Service Worker 中是否还有其他条件可以检查是否已应用更新?
The following seemed to work:以下似乎有效:
this.swUpdate.versionUpdates.subscribe((event: VersionEvent) => {
if (event.type === 'VERSION_READY') {
// do stuff
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.