I need to add a static image as shown below.Can you tell me why I cannot show the image on home page as shown below ? i.e. It's not working.
Here I'm using this ASP.NET Core Template Pack
Here is nice article about it from Steven Sanderson
\home\home.component.html
<img src="{{heroImageUrl}}" style="height:30px">
home.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'home',
template: require('./home.component.html')
})
export class HomeComponent {
public heroImageUrl ="./image/employee_management.jpg";
}
Error :
it says like this Failed to load resource: the server responded with a status of 404 (Not Found)
.May be a path issue.how can I give it correctly ? Image is there as shown above.
To use publish, create your JavaScript project using Visual Studio 2022 version 17.3 or later. In Solution Explorer, right-click the ASP.NET Core project and choose Add > Project Reference. Select the Angular project and choose OK. Right-click the ASP.NET Core project in Solution Explorer and choose Unload Project.
Run dotnet run to start the app. The project template creates an ASP.NET Core app and an Angular app. The ASP.NET Core app is intended to be used for data access, authorization, and other server-side concerns. The Angular app, residing in the ClientApp subdirectory, is intended to be used for all UI concerns.
Select the template, add your project name, and click OK to create your Angular 7 application using ASP.NET Core. You can see that a new Angular 7 project has been created. Also, we can see the ASP.NET Core Controller and Angular 7 Client App folder from the Solution Explorer.
Since you're using Webpack to bundle these files, you just need to use require
. Change your TypeScript code to this:
public heroImageUrl = require("./image/employee_management.jpg");
... and you're done. Your existing Webpack configuration is already set up to bundle .jpg
files using file-loader
, so the require
call will return the URL of the bundled image.
Note: The OP didn't mention, but they are using the ASP.NET Core + Angular 2 template here, which has Webpack all set up already. Therefore this ends up being a a Webpack question, not an Angular question, so the question title is misleading.
Include image folder in angular-cli.json under assets node like below
"assets": [
"assets",
"favicon.ico",
"fonts",
"images"
],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With