微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Angular Service Worker 在不应该更新时进行更新

如何解决Angular Service Worker 在不应该更新时进行更新

我有一个 angular 应用程序,并且在我的 app.component 中包含了一个 update.component。在这个组件中,我实现了一个服务工作者。它看起来像这样:

import { OnInit,Component } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';
import { interval } from 'rxjs';

@Component({
    selector: 'update-component',templateUrl: 'update-component.html'
})
export class UpdateComponent implements OnInit {
  constructor(
    public update: SwUpdate,) {}

  currentVersion: string;
  availabLeversion: string;

  ngOnInit() {
    this.initUpdate();
    this.checkUpdate();
  }

  initUpdate() {
    if(this.update.isEnabled) {
        this.update.available.subscribe((event) => {
            var r = confirm("Do you want to update your app?");
            if (r == true) {
                this.updateApp();
            }
        })
    }
  }

  updateApp() {
    this.update.activateUpdate().then(() => document.location.reload());
  }

  checkUpdate() {
    const timeInterval = interval(10 * 1000);
    timeInterval.subscribe(() => {
        this.update.checkForUpdate()
    });
  }
}

因此,如果我用新版本替换当前的 angular 版本,我的应用程序会询问用户是否要更新。如果是,页面将使用新版本的应用程序刷新。如果用户单击取消,则没有任何反应。到目前为止一切顺利。

但如果用户点击取消然后刷新页面,应用程序仍然会更新。这是认行为吗?如果是这样,我应该配置什么才能让我的用户有机会不更新?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。