我有一个像这样的vue文件,
export default {
data(){
return{
info: {
name: '',
image: '',
},
errors: []
}
},
created: function(){
this.getInfo();
},
methods: {
getInfo: function(){
this.info.name = response.data.results[0].name;
this.info.image = response.data.results[0].image;
}
}
}

我将此文件中的数据传递给子Vue组件。子组件如下,
<template>
<div class="ui items">
<div class="item">
<div class="ui small image">
{{info.image}}
</div>
</div>
</div>
</template>
<script>
export default{
props:['info']
}
</script>
&#13;
我的图像作为blob存储在MySQL数据库中。当我运行我的应用程序时,图像在UI上显示为二进制数据。对象看起来像这样,
这里有人可以帮我显示图像吗?非常感谢你!
答案 0 :(得分:2)
你想要的是data url。您需要将字节数组转换为base64。无法使用原始字节。也许使用one of the byte array to base64 functions在计算属性中执行此操作。
<强>标记强>
<img :src="dataUrl">
行为(未经测试!)
computed : {
dataUrl(){
return 'data:image/jpeg;base64,' + btoa(
new Uint8Array(this.info.image)
.reduce((data, byte) => data + String.fromCharCode(byte), '')
);
}
}
搜索你的良心。将图像作为JSON编码的字节数组发送是我从未见过的,并将猜测,比二进制图像大10倍。 Images in the DB are an antipattern。 JSON中的图像工作,但降低了JSON的可读性,并且可以像Postman一样埋葬工具。要扩大规模,首先要做的是将媒体(照片)与应用分开。这种技术应该保留用于缩略图,而你的45k图像不是缩略图。
答案 1 :(得分:1)
您可以使用Base64
格式加载图片数据,例如
<img src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo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yCMQEbp2WpU67IjfNjwcYyoUDhjJVcZBjYBy40j4wXgaobWoe8Z6Y80CJBwFpunepIzt2AUgFjtXXshNXjVmMh+K+zzp/CMs0CqeuzrxSRpbOKfdCkiMTS1VBQ41uxMyQR2qbrXiiwYN3ACh1FDmsdK2Eu4J6Tlo31dYVtCY88h5ELZIJJ+IRMzBHfyJINrigNkt5VsRiub9nXICdsYyVd2NcVvA3ScE5t2rb5JuEeyZnAhmLt9NK63vX1O5Pe8XaPSuGq1uTrfUgMEp9EJ+CQvr+BJ/AAKvAcCiAR+bf9CjAAluzmdX4AEIIAAAAASUVORK5CYII=">
对于你的问题,
export default {
data(){
return{
info: {
name: '',
image: '',
},
errors: []
}
},
created: function(){
this.getInfo();
},
methods: {
getInfo: function(){
this.info.name = response.data.results[0].name;
this.info.image = 'data:image/jpeg;base64,' + btoa(response.data.results[0].image.data);
}
}
}
在模板中,
<template>
<div class="ui items">
<div class="item">
<div class="ui small image">
<img :src="info.image">
</div>
</div>
</div>
</template>
答案 2 :(得分:-1)
<img :src="require(`../../assets/images/products/${product.img}`)" />