简体   繁体   English

带有导入图像的模板文字

[英]template literals with import images

<img alt="placeholder" src={ALL.file1.default}></img>

The above works and renders a picture.以上工作并呈现图片。

I'm trying to use template literals to make the 1 a dynamic variable.我正在尝试使用模板文字使 1 成为动态变量。

<img alt="placeholder" src={`${ALL.file`${randomnum}`.default}`}></img>
<img alt="placeholder" src={ALL.file`${randomnum}`.default}></img>

Both these don't seem to work as I get an error当我收到错误时,这两个似乎都不起作用

Attempted import error: 'file' is not exported from '../images/icons/' (imported as 'ALL').

I imported all the pictures using a index.js in the images folders exporting all of them individually then importing them with:我使用图像文件夹中的 index.js 导入所有图片,单独导出所有图片,然后使用以下命令导入它们:

import * as ALL from "../images/icons/";

         

Am I using the template literal incorrectly, or does it have something to do with the import conflicting method?我是否错误地使用了模板文字,还是与导入冲突方法有关?

Any help would be appreciated.任何帮助,将不胜感激。

Thanks in advance.提前致谢。

Remember when you use dynamic properties you need to use the [] syntax.请记住,当您使用动态属性时,您需要使用[]语法。

In your example, it would go like this:在您的示例中,它会像这样 go :

<img alt="placeholder" src={ALL[`file${randomnum}`].default}></img>

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

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