是否可以从 angular 中的应用程序代码强制硬应用程序更新(Ctrl + F5 浏览器模拟)?

Is it possible to force hard application update (Ctrl + F5 browser analogue) from the app code in angular?

我的 angular 应用有时会更新。 该应用程序已经上线,一些常见的使用场景是用户根本没有关闭应用程序的浏览器选项卡,因此可以永远使用没有检查的过时版本。

我已经实现了一种在后端检查应用程序版本的机制,因此所有来自过时版本的调用都会被拒绝。

下一步是向用户显示一些 "Update required" 对话框,以防出现此类后端拒绝和确认,最好不要强制用户手动按 "Ctrl + F5",而是从应用程序自动执行。

可能吗?

我们需要使用拦截器来检测错误,如果检测到错误,我们将使用 window.location.reload(true) 重新加载页面;它将删除缓存并重新加载页面。

@Injectable()
export class myInterceptor implements HttpInterceptor {

  constructor() { }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(request).pipe(
      catchError(error => this.handleError(error))
    );
  }

  private handleError(error: HttpErrorResponse): Observable<any> {
     if (error.status === 404) {
      // Do your thing here      
     window.location.reload(true);
   }         
  }

希望对您有所帮助。