vue.js - 参数化 v-for 指令
问题描述
我v-for
用来生成一组 vuetify v-timeline-item
,其中的元素根据存储在自定义对象中的值进行自定义:
<v-timeline-item
v-for="target in targetsList"
:key="target.uid"
:color="typesConfig[target.format].color"
:icon="target.icon"
large
fill-dot
>
我想typesConfig
根据 的值从对象中选择一种颜色target.format
。
typesConfig
是一个如下所示的计算属性:
const typesConfig = {
doc: {
color: 'red lighten-2',
icon: 'mdi-star',
}, etc...
我无法弄清楚如何从对象中获取颜色值并将其分配给:color
. 我尝试了各种各样的事情,包括没有运气的字符串文字:(
我希望上述内容很清楚,您的帮助将不胜感激。
谢谢你。
解决方案
你说typesConfig 是一个看起来像这样的计算属性:......但这不是计算属性的样子。
看起来您已经在 Vue 对象之外定义了一个常量。
我使用数据属性模拟了您的组件,typesConfig
并且您拥有的模板可以完美运行。
如果您从其他东西进行计算,例如targetsList
,您只需要一个计算属性,但您打算从这些对象中删除颜色和图标。
因此,您可能只需要一个从您的 const 初始化的数据属性,例如
const typesConfig = {
doc: {
color: 'red lighten-2',
icon: 'mdi-star',
},
}
export default {
data: () => ({
targetsList: [
{ uid: 'ABC', subject: 'S1', format: 'doc', notes: "some notes", date: 'Feb 10, 2020' },
{ uid: 'def', subject: 'S1', format: 'doc', notes: "some notes", date: 'Feb 10, 2020' },
{ uid: 'ghi', subject: 'S1', format: 'doc', notes: "some notes", date: 'Feb 10, 2020' }
],
typesConfig: typesConfig
})
};
推荐阅读
- java - 非静态字段在只有一个实例的类中是否有意义
- javascript - Javascript:为 HTML 中的树视图解析 JSON
- google-cloud-platform - Google Cloud Platform - Google Ads API 配额不可用
- c# - 使用不同的格式参数格式化金额
- python - 视差滚动弄乱了射弹的位置
- sql - SQL 中 TOTAL 的百分比
- serilog - 如何将磁盘上的 Serilog 文件接收器写入的数据推送到另一个 Serilog 接收器?
- wordpress - $ 不是一个函数 - 如何解决它
- python - 如何使用 Selenium python 录制视频
- java - Android 中的 Apache Velocity(“无法找到资源”)