我在博客后端的一个部分(不是 SPA)上使用 VueJS 2.5.3,该部分进行 API 调用以检查附加到帖子的特色图像。
如果找到,它会使用一个子组件来显示图像。问题是 API 调用成功后子组件没有渲染,因此图像对象也没有传递给它。
正如你在这个 GIF 中看到的,子组件没有渲染<!---->
,我有一个v-if
检查图像是否存在。但是,如果我单击 Vue DevTools 内的子组件,子组件会按预期呈现并显示图像。
我的问题是为什么只有在 Vue Devtools 中单击子组件后才会呈现子组件?当您单击组件时,Vue Devtools 是否会触发某种事件?
这是子组件:
<template>
<div v-if="showImage" class="featured-image-container" :class="[ size ]">
<img :src="processedSrc" alt="Featured Image">
</div>
</template>
<script>
export default {
props: {
image: {
type: Object
},
size: {
type: String,
required: true
}
},
data () {
return {
showImage: false
}
},
computed: {
processedSrc: function () {
if (this.image && typeof this.image === 'object') {
this.showImage = true
return this.image.sizes[this.size].file
} else {
this.showImage = false
}
}
}
}
</script>