Pet*_*xey 5 typescript service-worker angular
我希望能够读取 Angular PWA 当前活动的哈希值。但是我看不到任何这样做的机制。
SwUpdate 对象提供可观察量,允许您在新版本激活或可用时读取当前版本的哈希值,但似乎没有公开任何用于获取当前版本哈希值的静态方法。
class SwUpdate {
available: Observable<UpdateAvailableEvent>
activated: Observable<UpdateActivatedEvent>
unrecoverable: Observable<UnrecoverableStateEvent>
isEnabled: boolean
checkForUpdate(): Promise<void>
activateUpdate(): Promise<void>
}
Run Code Online (Sandbox Code Playgroud)
我希望能够读取类似SwUpdate.current读取当前哈希值(例如a518f9f1ab65954c2eafa02fec134fa54b391651)的内容,而不必等待available或activated事件。这可能吗?
您可以使用SwUpdatefrom@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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
927 次 |
| 最近记录: |