vue.js - Vue.js pass local loop variable to v-bind:style
问题描述
I loop through a list and would like to set a background image according to a property in this list.
But in v-bind:stlye
the property isn't defined.
How can I pass it?
<div class="content" v-bind:key="slide.id" v-for="slide in show.slides">
<div class="slide">
<div class="model"
:style="{ backgroundImage:
`url(${strapiUrl + slide.model_media.Media.url})` }">
<div class="title">{{slide.title}}</div>
</div>
</div>
解决方案
Perhaps it is simply easier to abstract the entire style binding into a method. In your template, you can simply do this:
<div class="model" v-bind:style="modelStyle(slide)">
Then, in your component, create a modelStyle()
method:
modelStyle: function(slide) {
return {
backgroundImage: url(`${this.strapiUrl}${slide.model_media.Media.url}`);
};
}
推荐阅读
- javascript - Vuejs在悬停时更改图像src
- android - 在 Android 应用程序中下载图像的最佳方法
- python - 为什么 python 对象被附加在列表的开头?
- c# - 如何在 C# 中使图表网格区域的背景透明且轴点字体颜色为白色。?
- html - 如何在没有样式的情况下将复制的文本插入到 Textarea?
- office-js - 安卓outlook web插件白屏问题
- java - Spring Boot控制器内的Access Token keycloak
- python - 在 django 应用程序中全局存储 oauth2 访问令牌的最佳方法?
- batch-file - wmic 输出到文件而没有额外的行问题
- excel - 使用 Application.Visible 属性时 Excel 应用程序在隐藏前闪烁