我希望能够读取我的 Angular PWA 的当前活动哈希。但是我看不到这样做的任何机制。
SwUpdate 对象提供了 observables,允许您在新版本激活或可用时读取当前版本的哈希值但似乎没有公开任何用于获取哈希值的静态方法当前版本。
class SwUpdate {
available: Observable<UpdateAvailableEvent>
activated: Observable<UpdateActivatedEvent>
unrecoverable: Observable<UnrecoverableStateEvent>
isEnabled: boolean
checkForUpdate(): Promise<void>
activateUpdate(): Promise<void>
}
我希望能够读取诸如 SwUpdate.current
之类的内容来读取当前哈希值(比如 a518f9f1ab65954c2eafa02fec134fa54b391651
),而无需等待 available
或 activated
事件。这可能吗?
答案 0 :(得分:0)
您可以使用 SwUpdate
中的 @angular/service-worker
来做到这一点。例如,您可以像这样在 app.component.ts
上检查:
import { Component, OnInit } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
constructor(private swUpdate: SwUpdate) {}
checkForUpdate() {
if (this.swUpdate.isEnabled) {
this.swUpdate.available.subscribe((event) => {
console.log('Update is available!');
console.log('Current version: ' + event.current.hash);
console.log('New available version: ' + event.available.hash);
});
}
}
ngOnInit() {
this.checkForUpdate();
}
}