【问题标题】:How do I bind a :src for an image in the child component?如何为子组件中的图像绑定 :src?
【发布时间】:2019-05-25 18:45:43
【问题描述】:

我正在尝试呈现组件列表。除 img src url 外,所有数据属性均正确显示。

文件/文件夹是:

CryptContent.vue - 包含 v-for

  • 要渲染的组件 assets/ - 包含图片

CryptContent.vue 包含:

<template>
<OwnedCardContent
            v-for="card in allCards"
            :id="card.id"
            :name="card.name"
            :cost="card.cost"
            :cset="card.cset"
            :edition_total="card.edition_total"
            :level="card.card_level"
            :unlock_czxp="card.unlock_czxp"
            :buy_czxp="card.buy_czxp"
            :transfer_czxp="card.transfer_czxp"
            :sacrifice_czxp="card.sacrifice_czxp"
            :url="card.graphic"
            :card_class="card.bg"
></OwnedCardContent>
</template>

<script>
allcards : [
      {id:0, name: 'Jim Zombie',graphic: './assets/jim.svg', cost: 300, cset: 'We like to party set', edition_total: ' of 100',unlock_czxp : '1,300,300',card_level: 80, buy_czxp: '1,800',transfer_czxp: '100', sacrifice_czxp: '2,300',bg: 'card-bg card-bg-6'},
]
 </script>`

OwnedCardContent.vue 包含:

<template>
<div id="1" :class="card_class">
            <img class="card-img" :src="url" />
            <span class="card-edition">#1{{edition_total}}</span>
            <div class="card-item-name text-center">{{name}}<br>{{cset}}</div>
            <div class="card-czxp text-left">{{unlock_czxp}}</div>
            <div class="card-level">{{level}}</div>
          </div>
        </div>
</template>

<script>
export default {
name: 'OwnedCardContent',
props: ['id','name','url','edition_total','cset','unlock_czxp','level','cost','buy_czxp','transfer_czxp','sacrifice_czxp','card_class'],
data () {
return {

    }
  }
}
</script>

图像不会渲染。当我检查代码时,来自 allCards 图形的正确值被注入到页面中..

当我删除 :src 并只输入 src="./assets/jim.svg" 时,它可以工作。

所以我认为这可能是 webpack 准备它的方式?我对此了解不够:(

任何帮助都会非常有帮助,谢谢!

【问题讨论】:

    标签: javascript html vue.js vuejs2 vue-component


    【解决方案1】:

    使用 webpack 图像被视为模块,因此您应该像这样导入或要求它们:

     allcards : [ {id:0, name: 'Jim Zombie',graphic: require('./assets/jim.svg'), ....]
    

      import img from './assets/jim.svg';
      export default{
           ...
             data(){
                 return {
                   allcards : [ {id:0, name: 'Jim Zombie',graphic: img, ....],
                     ...
                    }
                 }
             ...
    
            }
    

    【讨论】:

      【解决方案2】:

      你能不能试试src的使用方法

      getImgUrl(path) {
          var images = require.context('../assets/', false, /\.png$/)
          return images('./' + path + ".png")
        }
      
      <div class="col-lg-2" v-for="pic in pics">
         <img :src="getImgUrl(pic)" v-bind:alt="pic">
      </div>
      

      【讨论】:

        【解决方案3】:

        你可以试试这个

        &lt;img v-attr="src:imgUrl"&gt;

        【讨论】:

          猜你喜欢
          • 2021-06-04
          • 1970-01-01
          • 1970-01-01
          • 2012-10-20
          • 2020-04-10
          • 1970-01-01
          • 1970-01-01
          • 2021-07-23
          • 1970-01-01
          相关资源
          最近更新 更多