Vue.js Vue建议在模板循环结构中存储变量?

Vue.js Vue建议在模板循环结构中存储变量?,vue.js,Vue.js,给定以下代码 // Assuming records is an array of objects // Record is of the form {"fname": "John", "lname": "Smith", "dob": 1499994497871} // Field mapper is [ { "name": "fname", "label": "First Name", "render": function(val){ r

给定以下代码

// Assuming records is an array of objects
// Record is of the form {"fname": "John", "lname": "Smith", "dob": 1499994497871}
// Field mapper is
[
    {
        "name": "fname",
        "label": "First Name",
        "render": function(val){ return val}
    },
    {
        "name": "lname",
        "label": "Last Name",
        "render": function(val){ return val}
    },
    {
        "name": "dob",
        "label": "Date of Birth",
        "render": function(val){ return new Date(val).toDateString()}
    }
]

// Assume findField returns the mapper instance for a given record in the records array

<div v-for="record in records">
    <div>
        <table class="table">
            <tbody>
                <tr v-for="(value, key) in record">
                    <th v-if="findField(key)">{{ findField(key).label }}</th>
                    <td v-if="findField(key)">{{ findField(key).render(value) }}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

一种方法是使用组件

Vue.component("row", {
  props:["map", "value"],
  template:`
    <tr>
      <th v-if="map">{{ map.label }}</th>
      <td v-if="map">{{ map.render(value) }}</td>
    </tr>
  `
})
Vue.component(“行”{
道具:[“地图”、“价值”],
模板:`
{{map.label}
{{map.render(value)}
`
})
并修改模板以使用它

<tr is="row" v-for="(value, key) in record" 
             :map="findField(key)"
             :value="value">
</tr>

console.clear()
常量字段映射=[{
“名称”:“fname”,
“标签”:“名字”,
“渲染”:函数(val){
返回值
}
},
{
“名称”:“lname”,
“标签”:“姓氏”,
“渲染”:函数(val){
返回值
}
},
{
“名称”:“dob”,
“标签”:“出生日期”,
“渲染”:函数(val){
返回新日期(val).toDateString()
}
}
]
常量记录=[{
“fname”:“John”,
“lname”:“Smith”,
“dob”:1499994497871
},
{
“fname”:“John”,
“lname”:“Smith”,
“dob”:1499994497871
},
{
“fname”:“John”,
“lname”:“Smith”,
“dob”:1499994497871
}
]
Vue.component(“行”{
道具:[“地图”、“价值”],
模板:`
{{map.label}
{{map.render(value)}
`
})
新Vue({
el:“应用程序”,
数据:{
记录
},
方法:{
findField(键){
返回fieldMap.find(m=>m.name==key)
}
},
})

一种方法是使用组件

Vue.component("row", {
  props:["map", "value"],
  template:`
    <tr>
      <th v-if="map">{{ map.label }}</th>
      <td v-if="map">{{ map.render(value) }}</td>
    </tr>
  `
})
Vue.component(“行”{
道具:[“地图”、“价值”],
模板:`
{{map.label}
{{map.render(value)}
`
})
并修改模板以使用它

<tr is="row" v-for="(value, key) in record" 
             :map="findField(key)"
             :value="value">
</tr>

console.clear()
常量字段映射=[{
“名称”:“fname”,
“标签”:“名字”,
“渲染”:函数(val){
返回值
}
},
{
“名称”:“lname”,
“标签”:“姓氏”,
“渲染”:函数(val){
返回值
}
},
{
“名称”:“dob”,
“标签”:“出生日期”,
“渲染”:函数(val){
返回新日期(val).toDateString()
}
}
]
常量记录=[{
“fname”:“John”,
“lname”:“Smith”,
“dob”:1499994497871
},
{
“fname”:“John”,
“lname”:“Smith”,
“dob”:1499994497871
},
{
“fname”:“John”,
“lname”:“Smith”,
“dob”:1499994497871
}
]
Vue.component(“行”{
道具:[“地图”、“价值”],
模板:`
{{map.label}
{{map.render(value)}
`
})
新Vue({
el:“应用程序”,
数据:{
记录
},
方法:{
findField(键){
返回fieldMap.find(m=>m.name==key)
}
},
})

通过“是”特殊属性学习了一些新东西!感谢学习了一些新的“是”特殊属性!谢谢