【发布时间】:2018-04-19 03:34:20
【问题描述】:
我在博客后端的一个部分(不是 SPA)上使用 VueJS 2.5.3,该部分进行 API 调用以检查附加到帖子的特色图片。
如果找到,它会使用一个子组件来显示图像。问题是 API 调用成功后子组件没有渲染,因此图像对象也没有传递给它。
As you can see in this 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>
And here is a link to the code for the parent and child components:
【问题讨论】:
-
能否给出相关代码?你的
v-if里有什么? -
@thanksd 当然,如果 API 调用返回图像,
v-if是我设置的简单布尔值。v-if="showImage"我可以确认找到图像时已正确设置为true。 -
@thanksd 我已将父组件和子组件的完整代码添加到原始问题的底部。
-
请将
PostFeaturedImage.vue组件的代码添加到问题中,而不仅仅是提供链接。这就是核心问题所在,如果链接断开,就很难判断是什么导致了问题。
标签: vue.js vuejs2 vue-devtools