绑定来自外部源的图像 [英] Bind image from external source

查看:71
本文介绍了绑定来自外部源的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Vue.js绑定来自外部数据源的背景图片。代码如下所示:

I'm attempting to bind a background image from an external data source through Vue.js. This is what the code looks like:

<div class="image-box border"
            :style="{ background: `url(`+ item.image +`) no-repeat center` }" 
            style="width: 220px; height: 220px">

我也尝试过这样做:

<div class="image-box border" 
            :style="{ 'backgroundImage': 'url(' + item.image + ')' }">

这是该网址在应用中的显示,没有错误:

This is what the url reads like in the app with no error:

background-image: url("http://localhost:8080/assets/images/shoe-1.png");

感谢您的帮助!

已满代码:

组件-

<template>
    <div class="product-card-box border">
        <div class="image-box border"
                :style="{ background: `url(`+ item.image +`) no-repeat center` }" 
                style="width: 220px; height: 220px">
             <!-- :style="{ 'backgroundImage': 'url(' + item.image + ')' }"> -->

        </div>
        <div class="info-box border">
            <div class="color-info product-info bold">{{item.colors.length}} color</div>
            <div class="product-name">
                <div class="product-info bold">{{item.name}}</div>
                <div class="product-info sub-info">{{item.gender}}'s Shoe</div>
            </div>
            <div class="product-price">
                <div class="product-info sub-info">${{item.price}}</div>
            </div>

        </div>
    </div>
</template>

数据-

const data = [
{
    name: 'SNKR 001',
    gender: 'Men',
    price: 100,
    sport: 'running',
    width: 'Wide',
    colors: ['black', 'white', 'green', 'pink'],
    sizes: [3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 14, 15],
    image: '../assets/images/shoe-1.png'
},
{
    name: 'SNKR 002',
    gender: 'Men',
    price: 100,
    sport: 'running',
    width: 'Wide',
    colors: ['black', 'white', 'green', 'pink'],
    sizes: [3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 14, 15],
    image: '../assets/images/shoe-1.png'
}
];
export default data;


推荐答案

data [] ,使用 require(path),以便在模板中正确解析路径:

In data[], use require(path) so that the path is correctly resolved in your template:

image: require('../assets/images/shoe-1.png')

这篇关于绑定来自外部源的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆