How to load image (and other assets) in Angular an project?

前端 未结 8 1029
猫巷女王i
猫巷女王i 2020-11-29 03:14

I\'m pretty new to Angular so I\'m not sure the best practice to do this.

I used angular-cli and ng new some-project to generate a new app.

In

相关标签:
8条回答
  • 2020-11-29 03:54

    It is always dependent on where is your html file that refers to the path of the static resource (in this case the image).

    Example A:

    src
    |__assests
       |__images
          |__myimage.png
    |__yourmodule
       |__yourpage.html
    

    As you can see, yourpage.html is one folder away from the root (src folder), for this reason it needs one amount of ../ to go back to the root then you can walk to the image from root:

    <img class="img-responsive" src="../assests/images/myimage.png">
    

    Example B:

    src
    |__assests
       |__images
          |__myimage.png
    |__yourmodule
       |__yoursubmodule
          |__yourpage.html
    

    Here you have to go u in the tree by 2 folders:

    <img class="img-responsive" src="../../assests/images/myimage.png">
    
    0 讨论(0)
  • 2020-11-29 03:57

    I fixed it. My actual image file name had spaces in it, and for whatever reason Angular did not like that. When I removed the spaces from my file name, assets/images/myimage.png worked.

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