这对我有用。这有助于浏览器检测到有新版本的应用程序。
App.component.ts:
import { Component } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(public updates:SwUpdate) {
updates.available.subscribe(event => {
console.log('current version is', event.current);
console.log('available version is', event.available);
});
updates.activated.subscribe(event => {
console.log('old version was', event.previous);
console.log('new version is', event.current);
});
updates.available.subscribe(event => {
updates.activateUpdate().then(() => this.updateApp());
});
}
updateApp(){
document.location.reload();
console.log("The app is updating right now");
}
}
和
ngsw.json:
{
"index": "/index.html",
"assetGroups": [
{
"name": "Your app",
"installMode": "prefetch",
"updateMode": "lazy",
"resources": {
"files": [
"/favicon.ico",
"/index.html",
"/*.css",
"/*.js"
]
}
},
{
"name": "assets",
"installMode": "prefetch",
"updateMode": "lazy",
"resources": {
"files": [
"/assets/**"
],
"urls":[
"https://urlsyouwanttocachefrom.com/**",
]
}
}
]
}
这样,每次更新应用程序时,浏览器都会自动重新加载。