vuejs2 - 如何在Vue上渲染图像对象

标签 vuejs2 nuxt.js

是否可以渲染具有 source 属性的图像对象?

<template>
  <div v-html="image">
    {{ image }}
  </div>
</template>
<script>
export default {
  data () {
    return {
      image: new Image(),
      loading: true
    }
  },
  props: ['source'],
  created () {
    console.log(this.image)
    if (this.image) {
      this.image.onload = function (event) {
        console.log(event)
      }
      this.image.src = this.image
    }
    // this.src = this.image
  }
}
</script>

<style>

</style>

我只是想知道 Prop 源是否已加载,然后我将渲染它。 否则我会渲染其他内容,但我没有将其包含在代码片段中。

谢谢!

最佳答案

你可以做到

<template v-if="source">
    <img :src="source"/>
</template>
<template v-else>
 //something else
</template>

或者使用占位符图像。

<template>
  <div>
    <img :src="image"/>
  </div>
</template>
<script>
export default {
  mounted: function() {
    if (this.source) { //is it empty
      this.image = this.source //replace placeholder
    }
   this.loading = false
  },
  data () {
    return {
      image: somePlaceholderImage,//url for placeholder image
      loading: true
    }
  },
  props: ['source'],
}
</script>

<style>

</style>

关于vuejs2 - 如何在Vue上渲染图像对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53318545/

相关文章:

javascript - Vue 在更改特定按钮后更新所有按钮文本

nuxt.js - 使用 "nuxi"CLI 的 Nuxt 生成命令在 Netlify 上失败但在本地失败

nuxt.js - 运行开发Nuxtjs时出错

typescript - TS2749 : 'XXX' refers to a value, 但在这里被用作类型。您的意思是 'typeof XXX' 吗?

vue.js - 如何对将 DOM 元素附加到 HTML 主体的 Vue 组件进行单元测试?

javascript - Vuejs 和 Vue.set(),更新数组

css - 动画图像到 div vue.js

javascript - 如何在 vue-router 中使用 Vuetify 选项卡

mongodb - 在nuxt中使用mongodb

javascript - 如何访问 NuxtJs 存储中的 localStorage?