在Angular中,如果遇到缓存问题,可以尝试以下解决方法:
import { HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT'
})
};
// 在请求中使用httpOptions
this.http.get(url, httpOptions);
import { Routes } from '@angular/router';
const routes: Routes = [
{
path: 'example',
component: ExampleComponent,
data: { cache: false } // 添加cache属性
}
];
然后在组件中根据data.cache的值,动态地为路由添加参数:
import { ActivatedRoute, Router } from '@angular/router';
constructor(private route: ActivatedRoute, private router: Router) { }
ngOnInit() {
const cache = this.route.snapshot.data.cache;
if (!cache) {
this.router.navigate([], { queryParams: { random: Math.random() } });
}
}
"outputHashing": "all"
这样,每次构建时文件名都会发生变化,浏览器就会重新下载更新的文件。
这些解决方法可以根据具体情况选择适合的方法来解决Angular 7缓存问题。