【发布时间】:2017-08-30 01:46:02
【问题描述】:
我想要达到的目标:
从受 jwt 令牌保护的源加载图像
服务器将图像作为 base64 字符串返回,我会将此字符串作为背景 url 加载到图像上
父组件:
<template lang="html">
<myImage v-for="photo in form.photos" :photo="photo" @delphoto="deleteImage"></myImage>
</template>
export default {
components: {
myImage,
},
data(){
return {
form: {
photos: [
{
id: 1,
thumbnail: "logo1.png"
},
{
id: 2,
thumbnail: "logo2.png"
},
{
id: 3,
thumbnail: "logo3.png"
},
{
id: 4,
thumbnail: "logo4.png"
},
]
},
}
},
methods: {
deleteImage(myphoto)
{
this.form.photos.splice(this.form.photos.indexOf(myphoto), 1);
}
}
}
myImage 组件
<template>
<div v-if="loaded" class="img">
<img class="albumimg" :style="{ background: img}" :title="title">
<p @click="delimage">delete</p>
</div>
<div v-else class="img">
<img class="loading" style="background: url('/images/spinner.gif')">
</div>
</template>
<script>
export default {
data(){
return {
img: null,
loaded: false
}
},
props: {
photo: {
required: true
},
title: {
default: ''
},
},
created() {
this.imgurl()
},
methods: {
delimage() {
this.$emit('delphoto', this.photo)
},
imgurl() {
this.$http.get("/api/images/" + this.photo.id).then(response => {
this.img = "url('" + response.data + "')"
this.loaded = true
}, response => {
});
},
}
}
</script>
现在,如果我从 form.photos 数组中删除一张照片(拼接),最后一张图像总是会被删除。 当我删除绿色图像时
当我检查 form.photos 数组时,删除了正确的图像,只有 myImage 组件中的 img 数据属性仍然是前一个数组位置 1 的旧值。
我能够通过在照片道具上添加手表并重新获取 base64 字符串来绕过这个问题,这会导致对每个图像的新获取请求
watch: {
photo: function (val) {
this.imgurl()
}
},
有没有办法移除一个数组项(子组件)并显示正确的结果,而无需再次在子组件中获取所有图像?
谢谢
【问题讨论】:
标签: javascript laravel vue.js vuejs2 vue-component