Vue.js 什么';使用v-for在Vue js中迭代$data的最佳方法是什么?
所以我有这个:Vue.js 什么';使用v-for在Vue js中迭代$data的最佳方法是什么?,vue.js,Vue.js,所以我有这个: var vm = new Vue({ el: '#app', data: { firstName: {text: null, error: null}, lastName: {text: null, error: null}, email: {text: null, error: null}, phone: {text: null, error: null}, message: {t
var vm = new Vue({
el: '#app',
data: {
firstName: {text: null, error: null},
lastName: {text: null, error: null},
email: {text: null, error: null},
phone: {text: null, error: null},
message: {text: null, error: null},
},
…
其中一个在数组上进行迭代,如下所示:
…
<li v-for="item in items">
…
…
…
但我如何在不将所有对象放入数组的情况下做到这一点?基本上,我想访问顶级数据对象。以下操作不起作用:
…
<li v-for="item in data">
…
…
…
您可以编写一个计算函数来呈现数组,并在vue html中使用它:
var vm = new Vue({
el: '#app',
data: {
firstName: {text: null, error: null},
lastName: {text: null, error: null},
email: {text: null, error: null},
phone: {text: null, error: null},
message: {text: null, error: null},
},
computed: {
dataArr() {
let arr = [this.firstName, this.lastName, this.email, this.phone, this.message]
return arr
}
}
…
在html部分,您只需调用:
<li v-for="item in dataArr">
计算变量会自动更新,因此数据的任何更改都会反映在计算数组中
希望它很适合您。您可以编写一个计算函数来呈现数组,并在vue html中使用它:
var vm = new Vue({
el: '#app',
data: {
firstName: {text: null, error: null},
lastName: {text: null, error: null},
email: {text: null, error: null},
phone: {text: null, error: null},
message: {text: null, error: null},
},
computed: {
dataArr() {
let arr = [this.firstName, this.lastName, this.email, this.phone, this.message]
return arr
}
}
…
在html部分,您只需调用:
<li v-for="item in dataArr">
计算变量会自动更新,因此数据的任何更改都会反映在计算数组中
希望它适合你