在Angular项目的index.html文件中,在
标签中加入以下meta标签,强制浏览器不缓存index.html文件:
另外,也可以通过给Angular应用的模块添加一个随机数作为版本号,来更好地解决缓存问题:
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { RouterModule,Routes } from '@angular/router';
import { APP_BASE_HREF } from '@angular/common';
@NgModule({
declarations: [
AppComponent
],
imports: [
RouterModule.forRoot([
{ path: '', component: AppComponent, pathMatch: 'full' }
])
],
providers: [
{ provide: APP_BASE_HREF, useValue: document.getElementsByTagName('base')[0].href },
{ provide: 'APP_VERSION', useValue: Math.random() } // 加入随机数作为版本号
],
bootstrap: [AppComponent]
})
export class AppModule { }
这样,在打包Angular应用时,就会自动创建一个带有随机数的文件名,从而强制浏览器重新加载新的版本。