vue.js - 如何更改 v-for 循环 vuejs 中项目的背景?
问题描述
我有这个数组
items: [
{
cardTitle: 'Mathew Slick attached 2 files',
dateTime: '15 min ago',
contentTitle: 'Article / Post',
contentValue:
'The history of video game is as interesting as a fairy tale. The quality of today’s video game was not at all there when',
},
{
cardTitle: 'Mathew Slick attached 2 files',
dateTime: '15 min ago',
contentTitle: 'Article / Post',
contentValue:
'The history of video game is as interesting as a fairy tale. The quality of today’s video game was not at all there when',
},
]
我想为这些项目中的每一个提供特定的背景,我该怎么做?
这是我的v-for
循环代码:
<div v-for="(item, index) in items">
<div :style="whatShouldIGiveItHere">
{{item}}
</div>
</div>
编辑:我试图用这个添加变体背景颜色,computed
但它不起作用:
colorVariant() {
const colorVar = ['red', 'blue', 'green']
colorVar.forEach((val) => {
return val
})
},
<div v-for="(item, index) in items">
<div :style="background:colorVariant">
{{item}}
</div>
</div>
解决方案
要为每张卡片添加不同的背景颜色,您可以在 items 数组中添加一个属性。
items: [
{
cardTitle: 'Mathew Slick attached 2 files',
dateTime: '15 min ago',
contentTitle: 'Article / Post',
contentValue:
'The history of video game is as interesting as a fairy tale. The quality of today’s video game was not at all there when',
bgColor: 'red',
},
{
cardTitle: 'Mathew Slick attached 2 files',
dateTime: '15 min ago',
contentTitle: 'Article / Post',
contentValue:
'The history of video game is as interesting as a fairy tale. The quality of today’s video game was not at all there when',
bgColor: 'green',
},
]
循环时添加这样的样式
<div v-for="(item, index) in items">
<div :style="`background-color: ${item.bgColor}`">
{{item}}
</div>
</div>
// 编辑 - 需要项目数组之外的背景颜色(虽然不是一个好方法)
bgColors: ['red', 'green'],
items: [
{
cardTitle: 'Mathew Slick attached 2 files',
dateTime: '15 min ago',
contentTitle: 'Article / Post',
contentValue:
'The history of video game is as interesting as a fairy tale. The quality of today’s video game was not at all there when',
},
{
cardTitle: 'Mathew Slick attached 2 files',
dateTime: '15 min ago',
contentTitle: 'Article / Post',
contentValue:
'The history of video game is as interesting as a fairy tale. The quality of today’s video game was not at all there when',
},
]
/* the loop will take the same index color and add it to the card. */
<div v-for="(item, index) in items">
<div :style="`background-color: ${bgColors[index]}`">
{{item}}
</div>
</div>
推荐阅读
- html - 如何让一个 div 左对齐,另一个右对齐。在移动端,右边上升左边下降
- python - 如何在 QtChart QLineSeries 中获取轴标签
- c - 如何计算 C 结构中的填充
- angular - 通过 HttpInterceptor 创建刷新令牌的问题
- sql - 如何在多个列上进行精确匹配并显示最新日期?
- python - 使用 c++ 时,Caffe python 预测不同
- statistics - 移除平均观测值后,方差如何变化?
- sql - 数据库复合主键中的顺序是否重要?
- javascript - 隐藏谷歌地图标记,直到被复选框激活
- c - 我犯了这个程序会通过运行时错误发生的错误是什么?