css - Angular 9中具有特定基本href的css中背景图像的路径
问题描述
angular.json 中的 baseHref 参数是 /iofrog/ + i18n.locales 中定义了 7 种语言,因此 index.html 中的最终 baseHref 是 /iofrog/en 用于英文构建。
在应用程序组件的 css 文件中,我有:
background-image: url('~/assets/img/background.jpg');
这由 Angular 9 CLI 转换为路径 /iofrog/assets/img/background.jpg,因此 /en/ 被跳过!它是 Angular CLI 中的错误吗?
我也试过:
background-image: url('/assets/img/background.jpg'); // not showing the image
background-image: url('./assets/img/background.jpg'); // compiler error
background-image: url('assets/img/background.jpg'); // not showing the image
我还找到了 --rebaseRootRelativeCssUrls=true|false 选项,但它已被折旧。
有效的解决方法是在 TS 中:
this.backgroundImageURL = `url(${this.baseHref}assets/img/background.jpg)`;
在 HTML 模板中:
[style.background-image]="backgroundImageURL ? backgroundImageURL : ''"
但这并不优雅,并且在呈现 html 后正在加载图像。
Angular 9 中的正确解决方案是什么?
解决方案
There were some issues with i18n building angular app and base href.
It should work with that syntax for scss files:
background-image: url('~src/assets/img/background.jpg');
Hope this helps.
推荐阅读
- android - 工具栏下方的白条
- java - Java 错误:输入在 Cicle 中不起作用
- ruby-on-rails - 搜索以小时为整数的数据库池
- plugins - FIJI / ImageJ:如何在另一个插件中使用一个插件的类?
- java - Storm bolt 无法从 spout 反序列化对象
- javascript - Telegram BOT - 如何添加图标?
- filter - jquery TableSorter - 如何重建或刷新下拉过滤器
- sql - SQL Server 存储过程 - 引用两个表
- r - 通过 RStudio 中的 sparklyr 连接 Livy,比通过 sparkmagic 连接慢
- java - %_gpg_name 未使用 maven rpm 插件设置