简体   繁体   English

使用 require 在 Angular 11 应用程序中加载图像

[英]Using require to load images in Angular 11 app

I was trying to load some images in an Angular 11 app in the traditional way by adding <img src="../xxx/yyy" into Html but it returned 404 error .我试图通过将<img src="../xxx/yyy"到 Html 中,以传统方式在 Angular 11 应用程序中加载一些图像,但它返回404 error (I'm sure that the path is OK). (我确定路径没问题)。 Afterwards, I tried the require solution but it failed again.之后,我尝试了 require 解决方案,但它再次失败。 This is the way I'm using require.这是我使用 require 的方式。 Any ideas-solutions?任何想法 - 解决方案?

Typescript打字稿

declare var require: any;
imgname= require("../../assets/images/Component95_1.png").default; 

Html html

<img src={{imgname}}>

You need to create an assets directory in src .您需要在src创建一个assets目录。 With full path you can get the image.使用完整路径,您可以获得图像。 example 例子

I recommend to set src via input directive: <img [src]="imgname">我建议通过输入指令设置src<img [src]="imgname">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM