0

I had an angular 6 project where we used to have CSS class for images like the below:

.some-class { background: url('asset/images/myimage.png'); 

This used to work without any issue irrespective of deployment and local development as well.

Lately, we upgraded the angular version to 15 for this project, where if we try to keep the above code as it is, the compiler complains Can't resolve './assets/_css/assets/images/myimage.png'

So we have tried this way:

.some-class { background: url('~asset/images/myimage.png'); 

Now this copies files from the asset folder to the root folder and provides the hash value of an image like myimage_2432432.png

We also tried below option:

.some-class { background: url('../../asset/images/myimage.png'); 

This is the same as stated in the second point, but we can use this as our application is embedded in some other portal as well keeping the file in the root folder won't work out.

As per the given architecture, seems like we can only render the image below way to the QA env:

.some-class { background('~/asset/image/myimage.png'); // Please note that this is after deployment as the app could be standalone or embedded into another portal 

Keeping in mind the above requirement, can anyone suggest anything?

Can we webpack would be a savior here?

1 Answer 1

0

Possible solutions:

  1. Try to use the relative path with the tilde sign (~) like below.

    .some-class { background: url('../../assets/images/myimage.png'); }

  2. Ensure that your angular.json or angular-cli.json file is correctly configured.

  3. Check the "assets" section in the build options to make sure your asset folder is included.

    "assets": [ "src/assets", "src/favicon.ico" ],

  4. Try using Angular CLI commands to build and serve your application. For example:

    ng build --prod

    ng serve --prod

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.