当我在加载时调用changeBgImage
函数时,背景图片不适用于此div。我在网上搜索但未获得所需的结果。请帮助我
<div class="home_search" id="home_search" v-bind:style="{ background: `url(${imageUrl}) no-repeat center` }">
<HeaderComponent />
</div>
changeBgImage () {
let rand = Math.round(Math.random() * 4);
this.imageUrl = "../assets/img/home_" + rand + ".jpg";
console.log(this.imageUrl)
}
答案 0 :(得分:1)
您必须在标签(或容器)中添加宽度和高度
示例: https://codepen.io/anon/pen/vzyjww?editors=1010
<div
:style="{ background: `url(${imageUrl}) no-repeat center` }"
style="width: 350px; height: 150px"
>
</div>