Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use angular base href as variable in image path?

Tags:

Using the Angular template built into VS 2017. I have an image in the assets/image folder, so ClientApp > src > assets > images.

I reference the image in my component like:

<img src="../../../assets/images/Logo.png" /> 

When deployed local, it works but after deploying to IIS it results in 404 error.

On the server, the app is deployed to a subfolder: inetsrv8/internal/MyApp

I have to change the BASE HREF before deploying to get the app to work, so I'm thinking a fix would be to build custom paths for the images along the lines of BaseHref + './pathToImage'. The problem is I can't figure out how to grab the BASE Href to put it in a variable.

Is there a better way? A proper way? If not, how would I get the base href?

like image 732
BattlFrog Avatar asked May 17 '18 21:05

BattlFrog


People also ask

What is the purpose of base href tag in Angular?

The <base href="/"> tells the Angular router what is the static part of the URL. The router then only modifies the remaining part of the URL. Now that we've set the our base href tag, we can then move on to defining some routes without your route module file or your app.

What is App_base_href?

APP_BASE_HREFlinkA predefined DI token for the base href to be used with the PathLocationStrategy . The base href is the URL prefix that should be preserved when generating and recognizing URLs.

What is deploy URL in Angular?

--deploy-url A second parameter that is important is '--deploy-url'. This parameter will update the generated url's for our assets(scripts, css) inside the index. html. To make your assets available at /angularapp/, the deploy url should be set to /angularapp/.


1 Answers

Try this.

<img src="./assets/images/Logo.png" />

Actually it depends on build structure. Best way is to check angular-cli configuration. Otherwise ng built and check where is your assert files.

like image 50
Ritwick Dey Avatar answered Sep 20 '22 19:09

Ritwick Dey