简体   繁体   English

如何绑定:样式与对象语法和多个背景图像?

[英]How can I bind:style with object syntax and multiple background images?

I want to bind css style with the object syntax to an image.我想将带有对象语法的 css 样式绑定到图像。 the style object contains the css property background refering to multiple background images.样式对象包含引用多个背景图像的 CSS 属性background Unfortunately, these images are not displayed.不幸的是,这些图像没有显示。

Template模板

<v-img
    :src="require('../assets/background/bg_0.png')"
    :style="bg"
></v-img>

Script脚本

data() {
    return {
        bg: {
            background: "url('~@/assets/pieces/animals/animal_1.png') no-repeat center, url('~@/assets/pieces/animals/animal_3.png') no-repeat center",
            width: "300px",
            height: "300px"
    }
}
}

The properties for width and height are working, but the two images from background are not displayed. widthheight的属性正在工作,但不显示来自background的两个图像。

What am i doing wrong?我究竟做错了什么?

Thanks for your help!谢谢你的帮助!

The background CSS is technically correct, but I believe your issue is the image path.背景 CSS 在技术上是正确的,但我相信你的问题是图像路径。 That path prefix ~@ won't work here because you haven't required the image so Webkit doesn't know about it.该路径前缀~@在这里不起作用,因为您不需要图像,因此 Webkit 不知道它。 My recommendation would be to require the image first and then pass it as a constant to the bg object.我的建议是首先需要图像,然后将其作为常量传递给bg对象。 That would clean things up by splitting code up over multiple lines and still allow you to use your path prefix for the dynamic URLs.这将通过将代码拆分为多行来清理内容,并且仍然允许您将路径前缀用于动态 URL。

Something like this像这样的东西

const animal1 = require('~@/assets/pieces/animals/animal_1.png')
const animal3 = require('~@/assets/pieces/animals/animal_3.png')
data() {
    return {
        bg: {
            background: `url(${animal1}) no-repeat center, url(${animal3}) no-repeat center`,
            width: "300px",
            height: "300px"
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM