javascript - Vue img 未在 v-for 列表中呈现
问题描述
我有一个要列出的对象数组,但是当我渲染图像 src 时,图像是空白的。我已经确认图像本身的路径是正确的,但我无法让它们显示
我正在关注https://codesource.io/rendering-lists-in-vue/作为教程,据说将其指定为 url,我尝试使用 https 和 http 作为 localhost 但图像仍然是空白
最后,我尝试使用函数手动 src 图像,但出现同样的问题,组件很小,所以我将它包含在这里
希望有人对我接下来可以尝试的内容提出建议:)
<template>
<div id="sideBarDiv">
<img src="../assets/Icon_3.png" height="40px" width="40px"/>
<div v-for="link in links" :key="link.id">
<p>.</p> <img v-bind:src="link.src" height="15px" width="15px"/>
</div>
</div>
</template>
<script>
export default {
name: 'sidebar',
data: function(){
return{
links:[
{
id: 0,
src: 'https://src/assets/superdash/ic_active-2.png',
isActive: false
},
{
id: 1,
src: 'https://src/assets/superdash/ic_active@2x.png',
isActive: false
},
{
id: 2,
src: 'https://src/assets/superdash/ic_active-1@2x.png',
isActive: false
},
{
id: 3,
src: 'https://src/assets/superdash/ic_active-3.png',
isActive: false
},
{
id: 4,
src: 'https://src/assets/superdash/ic_active-4.png',
isActive: false
}
]
}
},
methods:{
getLinkImage(img){
return ('@/assets/superdash/'+img+'.png'); //attempt to plugin image name and combine with known path to render
},
}
}
</script>
解决方案
尝试从方法中删除路径:
new Vue({
el: '#demo',
name: 'sidebar',
data(){
return{
links:[
{id: 0, src: 'https://picsum.photos/200', isActive: false},
{id: 1, src: 'https://picsum.photos/200', isActive: false},
{id: 2, src: 'https://picsum.photos/200', isActive: false},
{id: 3, src: 'https://picsum.photos/200', isActive: false},
{id: 4, src: 'https://picsum.photos/200', isActive: false}
]
}
},
methods:{
getLinkImage(img){
return (img);
},
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<div id="sideBarDiv">
<img src="../assets/Icon_3.png" height="40px" width="40px"/>
<div v-for="link in links" :key="link.id">
<p>.</p> <img v-bind:src="link.src" height="45px" width="45px"/>
</div>
</div>
</div>
推荐阅读
- types - 将类型转换为字符串
- c++ - 为什么 EdsDownloadEvfImage() 总是返回 EDS_ERR_OBJECT_NOTREADY
- kubernetes - Redis 似乎在启动时删除了 dump.rdb。使用 Kubernetes PVC 和 KubeDB。为什么会这样?
- reactjs - 我正在尝试将 Firebase 电子邮件验证链接验证添加到我的应用程序,但出现错误“无法读取 null 的电子邮件属性”
- android - for循环中的变量是不可识别的,而不是在循环中声明和初始化的
- mysql - 如何在不破坏mysql中表排列的情况下选择表中特定的eid
- android - 在Android webview上限制html输入类型时间的宽度
- ios - 如何确定当前设备是否具有支持 P3 的显示器?
- angular - 如何读取配置文件以在打包为 WAR 并部署在 IBM Liberty 上的 Angular 应用程序中设置 API URL
- entity-framework - EF SQL WHERE 使用 LOWER() 转换进行字符串比较