Assets not found in scss/css when using base-href
See original GitHub issuePlease provide us with the following information:
OS?
Windows 10 x64
Versions.
@angular/cli: 1.0.0-beta.32.3 [1.0.0-beta.28.3] node: 7.2.0 os: win32 x64 @angular/common: 2.4.7 @angular/compiler: 2.4.7 @angular/core: 2.4.7 @angular/forms: 2.4.7 @angular/http: 2.4.7 @angular/platform-browser: 2.4.7 @angular/platform-browser-dynamic: 2.4.7 @angular/router: 3.4.7 @angular/cli: 1.0.0-beta.32.3 @angular/compiler-cli: 2.4.7
Repro steps.
create an app with ng-cli. create a build for a server (not ng serve) with base-href set to xxx. use url’s in scss (like background-image: url(‘/assets/img/myimg’). the imagepath for the url will be set to server/assets/img/myimg instead of server/xxx/assets/img/myimg. If I try to use a relative path in the scss (ie url(‘./assets/img/myimg’) ) i get a compileerror, that this is not resolveable. If i try to use url(‘assets/img/myimg’) this is also not resolveable for the compiler. my .angular-cli.json has this section: “assets”: [ “assets”, “favicon.ico” ],
The log given by the failure.
There is only a compileerror that the path is not resolveable or a runtime 404 when the resource is not found without using the base-ref in the url.
Mention any other details that might be useful.
There is a workaround for this issue: i set the style in my component directly and inject it like this: <span … [style.background-image]=“getLogo()”></span>
getLogo() {
return this._domSanitizer.bypassSecurityTrustStyle(url(${this.logosrc});
}
where logosrc = “./assets/img/myimg”
Thanks! We’ll be in touch soon.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:11
- Comments:26 (4 by maintainers)
Top Related StackOverflow Question
fhbgjtgyjhuyhj
@chriseenberg this actually works fine for me: simply use
<img src="./assets/img/myimg">and make sure your assets folder is set in .angular-cli.json in the assets section. I’ve used this pattern also in the scss-files, but the newest version broke this and i get a compile-error that those path’s cannot be resolved.