ng build --base-href=“/…/” --deploy-url=“/…/” css background image not working

跟風遠走 提交于 2020-08-03 06:39:09

问题


"@angular/core": "~8.0.0",

scaffold angular 8 project with scss style option

i have code for as mentioned below in component scss file

background: url("/assets/images/vector-icon.png") no-repeat 25%;

at the final buil using command

ng build --prod --base-href="/website/" --deploy-url="/website/"

put build at sub folder root/webside

everything working fine except background image path

i have tried by ip/website/assets/images/vector-icon.png at that point i am able to get image.

What if i don't wont to change the path at every css file.

any help would be appreciated. thanks in advance


回答1:


The solution is to put "rebaseRootRelativeCssUrls": true in the angular.json file.

The / before assets is correct.( url(/assets/img...) )

 "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
           ...
           "rebaseRootRelativeCssUrls": true




回答2:


Don't use / before assets

background: url("assets/images/vector-icon.png") no-repeat 25%;

PS. And the assets/images folder has to contain this image.

PPS. Make sure this path is correct.




回答3:


Faced this problem myself. Instead of /assets..., use src/assets.... Make sure you don't put / before src.




回答4:


First thing you need to check that go to assets folder and check that file exists or not. If file exists, try below line

background: url('./assets/images/vector-icon.png') no-repeat 25%;



回答5:


Try this:

background: url("/src/assets/images/vector-icon.png") no-repeat 25%;


来源:https://stackoverflow.com/questions/56625979/ng-build-base-href-deploy-url-css-background-image-not-worki

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!