可以尝试在应用程序启动时手动检查更新,以确保更新检查成功。以下是一些示例代码:
import { Component, OnInit } from '@angular/core'; import { SwUpdate } from '@angular/service-worker';
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit {
constructor(private swUpdate: SwUpdate) { }
ngOnInit() { if (this.swUpdate.isEnabled) { this.swUpdate.checkForUpdate().then(() => { console.log('App Checked for update'); }).catch((err) => { console.error(err); }); } } }
在这个例子中,我们在AppComponent的ngOnInit方法中手动检查更新。如果更新可用,则SwUpdate.activated应该被触发。