只需更新到最新版本的 Angular 17 和 @angular/[电子邮件受保护],现在当我开始时就会弹出此消息。我之前运行过@angular/[电子邮件受保护]。
Error: src/app/app.component.ts:44:21 - error TS2339: Property 'available' does not exist on type 'SwUpdate'.
44 this.swUpdate.available.subscribe(() => {
这是我的代码:
constructor(
...
private swUpdate: SwUpdate
) {
}
ngOnInit(): void {
...
if (this.swUpdate.isEnabled) {
this.swUpdate.available.subscribe(() => {
window.location.reload();
});
}
有什么建议吗?
在新版本中找不到任何关于此问题的信息。
查看
16.2.12
版本中的文档,available
属性被标记为已弃用,并且确实已在版本17中删除。
文档建议使用
versionUpdates
Observable 来代替
/**
* Emits an `UpdateAvailableEvent` event whenever a new app version is available.
*
* @deprecated Use {@link versionUpdates} instead.
*
* The behavior of `available` can be replicated by using `versionUpdates` by filtering for the
* `VersionReadyEvent`:
*
* {@example service-worker-getting-started/src/app/prompt-update.service.ts
* region='sw-replicate-available'}
*/
readonly available: Observable<UpdateAvailableEvent>;
查看文档,我会将代码替换为以下内容:
this.swUpdate.versionUpdates.subscribe((event) => {
if (event.type === "VERSION_READY") {
window.location.reload();
}
});