I'm using Angular 9. A scss file for a header contains:
.avatar {
...
background-image: url('assets/images/avatar.png');
...
This generates a compile error:
ERROR in ./src/app/header/user-dropdown/user-dropdown.scss
Module Error (from ./node_modules/postcss-loader/src/index.js):
(Emitted value instead of an instance of Error) CssSyntaxError: ...clientapp\user-dropdown.scss:10:2: Can't resolve './assets/images/avatar.png' in '....clientapp\src\app\header\user-dropdown'
> 10 | background-image: url('./assets/images/avatar.png');
| ^
However, if I put a "/" in front of the url() argument, it compiles:
.avatar {
...
background-image: url('/assets/images/avatar-image.png');
...
But then the image does not display in production, where the location of assets is not found. (I am deploying to Github pages).
However, I can use the url() method without the "/" in all other places in my code where I set the background image programmatically. And then the image is found both locally and in production.
header.ts
backgroundStyle: any;
...
const background = "url('assets/images/some-image.png');
this.backgroundStyle = { 'background-image': background };
header.html
<mat-toolbar class="main-header" [ngStyle]="backgroundStyle">
What syntax should I use within the scss file?