angular - 将哈希添加到 css 文件名
问题描述
背景
- 我有两个css文件-styles.mobile.css和styles.desktop.css
- 顾名思义,我为移动和桌面视图使用了两个单独的 css
- 我根据屏幕大小在 index.html 中动态注入 css
- 显然我不能把它们放在styles.css
- 我附上了文件夹结构截图
问题 当我运行 ng build --prod 时,有没有办法向styles.mobile.css 和styles.desktop.css 添加散列
检测设备和注入 css 的代码
private addCss(): void {
if (this.currentDevice === "desktop") {
this.document
.getElementById("theme")
.setAttribute("href", "styles.desktop.css");
} else {
this.document
.getElementById("theme")
.setAttribute("href", "styles.mobile.css");
}
}
解决方案
添加“ng build --prod --output-hashing all”,对生成文件的内容进行hash,并将hash附加到文件名,以方便浏览器缓存破坏
推荐阅读
- powershell - 如何在powershell脚本中获取对象的键和值数组?
- macos - 如果通过(硬)符号链接修改文件,如何检测(FSEvent)?
- javascript - 选择文件目录路径?html 的
- javascript - TS - 将 id 映射到对象属性
- c - 从基本 C 脚本打印输出
- http - 由 HTTP 增强的 ABNF:#symbol 的规范性参考
- reactjs - axios拦截器如何被触发
- sql - SQL Server 中的数据透视记录
- echarts - ECharts - 无法在时间轴(xAxis)上创建垂直标记线
- r - 在 R 中解析 URL 以提取特定数据