Set Image path on ASP.NET Core + Angular 2 template for Visual Studio

后端 未结 5 989
误落风尘
误落风尘 2020-12-16 03:43

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 AS

相关标签:
5条回答
  • 2020-12-16 04:07

    Two things to note, first use brackets for the src property:

    <img [src]="heroImageUrl " />
    

    Second, make sure the url of the image is relative to the url of the route that is used to display the component. Because this is generally not a great thing to do, I'd recommend making the url absolute from the root of the application:

    public heroImageUrl ="/ClientApp/app/components/home/image/employee_management.jpg";
    

    And then better yet, place it in a common location like /images/employee_management.jpg

    0 讨论(0)
  • 2020-12-16 04:10

    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.

    0 讨论(0)
  • 2020-12-16 04:14

    Include image folder in angular-cli.json under assets node like below

     "assets": [
            "assets",
            "favicon.ico",
            "fonts",
            "images"
          ],
    
    0 讨论(0)
  • 2020-12-16 04:16

    Depending on what your setup is, you probably have to place the static data files in a ressource/assets folder.

    My setup places the root of my site in /src. I place angular files in the subfolder /src/app and if i want to link to images i place those in a subfolder of src called /src/assets. When linking those images, i simply write the path as if /src is the root, so linking to an image called employee_management.jpg would be in

    'assets/employee_management.jpg'
    

    I use the Angular CLI btw, which uses webpack to bundle it all.

    0 讨论(0)
  • 2020-12-16 04:20

    you should use

    <img [attr.src]="heroImageUrl" style="height:30px">
    

    or

    <img attr.src="{{heroImageUrl}}" style="height:30px">
    

    also make sure path should be relative.

    Angular by default uses property binding. To tell Angular explicitly to use attribute binding.

    0 讨论(0)
提交回复
热议问题