typescript - src 属性中的 VueJS 插值
问题描述
在带有 TypeScript 和 Vuetify 应用程序的 VueJS 中,我有十张图片,分别称为 picture1.png、picture2.png、picture3.png 等。我正在尝试<div><img src="../assets/pictures/picture1.png"/></div>
为每张图片制作。这是我的代码:
<template>
<div class="home">
<div class="pictures">
<div v-for="index in Array.from({length: 10}, (v, k) => k+1)" :key="index">
<img src='../assets/pictures/picture{{index}}.png'>
</div>
</div>
</div>
</template>
这有一个错误,即属性中的插值已被删除。正如回答这个问题所解释的那样,您应该使用 v-bind 代替:
<template>
<div class="home">
<div class="picture">
<div v-for="index in Array.from({length: 10}, (v, k) => k+1)" :key="index">
{{index}}
<img :src="'../assets/pictures/picture' + index + '.png'">
</div>
</div>
</div>
例如,输出十张损坏的图像, src 字面意思是这样的:
src="../assets/pictures/picture1.png"
并且该文件不存在,因此它显示损坏的图像图标。
如果我只使用<img src='../assets/pictures/picture1.png'>
,它可以工作,并且 dom 检查器中显示的图像 url 是<img src="/img/picture1.ea361a2e.png">
.
有没有办法src
在 Vue 中动态地正确处理构建 img s,但仍然允许它以处理普通src
s 的方式处理它而无需动态绑定?
解决方案
我很幸运地做了以下事情。
:src="require(`images/image-${index}.png`)"
推荐阅读
- javascript - 如何清理这个 useEffect 钩子
- javascript - 如何通过在单击按钮时覆盖已打开的选项卡来打开新选项卡?
- amazon-web-services - 如何在 AWS 上启动 Snakemake 工作流程并分离?
- python - 如何在 OWL 中获取文字标签值?
- prolog - 由于空间不足而无法打印资源异常-SWI-prolog
- javascript - 制作一个固定的导航栏,Uncaught TypeError?
- php - 如何使用 php wordpress 使用此代码在特定页面上插入按钮
- python - 如何将 RGB 图像转换为灰度,扩展该灰度图像的尺寸以在 InceptionV3 中使用?
- api - 如何从 Platform of Trust 获取 Sandbox 环境中当前登录用户的 ID?
- sql - 带有 OUTPUT 的存储过程 - 必须声明标量变量