javascript - Vue.js 组件图像空白

标签 javascript image vue.js src

我已将计算属性绑定(bind)到 Vue.js 中图像标记的 src 。该代码似乎是正确的,但不能一致地工作,这让我感到困惑。此外,切换到 /about 页面并返回主图书页面始终可以正确显示图像。

任何有关可能导致此问题的原因的信息都会很棒!

此处提供了该应用程序的托管版本:https://books.surge.sh/

The relevant code for the book-item component.

The full Github repo.

生成书籍组件和图像src的代码如下:

<template>
  <article class="book-item">
    <img :src="imgSrc" class="image">
    <div class="meta">
      <div class="name">{{ book.title }}</div>*
      <div class="author">{{ book.author }}</div>
    </div>
    <div class="description">
      <p v-html="book.description"></p>
    </div>
  </article>
</template>

<script>
export default {
  props: ['book'],
  computed: {
    imgSrc() {
      return `/static/img/${this.book.image}`;
    }
  }
};
</script>

初始加载时部分显示的书籍封面:

Partially displayed images.

最佳答案

问题出在 .image 'width' 样式上。您应该在组件渲染后分配该 .image 类。这样做:

<template>
  <article class="book-item">
    <img :src="imgSrc" :class="{image: componentLoaded}"> <!-- 1. change class -->
    <div class="meta">
      <div class="name">{{ book.title }}</div>*
      <div class="author">{{ book.author }}</div>
    </div>
    <div class="description">
      <p v-html="book.description"></p>
    </div>
  </article>
</template>

<script>
export default {
  props: ['book'],
  data() {
    return {
      componentLoaded: false           // 2. add this 'state' data
    };
  },
  computed: {
    imgSrc() {
      return `/static/img/${this.book.image}`;
    }
  },
  mounted() {
    // 3. This will assign class to your image
    // after component did mounted (nextTick() did not helped)
    setTimeout(() => {
      this.componentLoaded = true;
    }, 1);
  }
};
</script>

祝你好运! 我创建了一个拉取请求 here

关于javascript - Vue.js 组件图像空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45365644/

相关文章:

javascript - 预期的 ;解析csv文件时

javascript - 强制页面内容为视口(viewport)的 100%

python - 我如何使用 Python 缩略图

html - 调整 Bootstrap 图像大小

vue.js - 我可以禁用特定组件的某些 Vue 警告吗?

javascript - 以编程方式在不同页面的 vue 组件中显示数据

javascript - 未捕获触发的按键事件?

c++ - 只有左边的图像显示在 OpenGL Stereo 上

javascript - vue-uuid 的 v-for 键的 uuid?

javascript - 如何访问动态属性: objectName. {variable}