[英]Vue.js - Values created in a method (on-click) not being displayed in a v-for loop
I want the program to fetch some data @click
and then display it in the v-for
loop.我希望程序获取一些数据@click
,然后将其显示在v-for
循环中。
After I click the button, data shows up in the console, but I don't see anything being displayed on the page.单击按钮后,数据会显示在控制台中,但我没有看到页面上显示任何内容。
Is it because data is being created after the element has been rendered?是因为数据是在元素渲染后创建的吗? How do I make Vue to re-render the loop to show the fetched data?如何让 Vue 重新渲染循环以显示获取的数据?
<v-btn @click="fetchData">
fetch propeties
</v-btn>
<h2>Properties:</h2>
<div v-for="item in properties">
<div>{{item.title}}: {{item.value}}</div>
</div>
...
data () {
return {
properties: {}
}
},
methods: {
fetchData () {
var nameObj = {'title': 'name'}
var sizeObj = {'title': 'size'}
nameObj['value'] = 'file.txt'
sizeObj['value'] = 12
this.properties['name'] = nameObj
this.properties['size'] = sizeObj
console.log(this.properties)
}
}
In documentation of vuejs it is clearly mentioned that v-for is for arrays and not objects.在 vuejs 的文档中明确提到 v-for 用于数组而不是对象。
https://v2.vuejs.org/v2/guide/list.html https://v2.vuejs.org/v2/guide/list.html
Your properties is an object which has name as size.您的属性是一个名称为大小的对象。
Make this change进行此更改
new Vue({ el: '#app', data () { return { properties: [] } }, methods: { fetchData () { var nameObj = {'title': 'name'} var sizeObj = {'title': 'size'} nameObj['value'] = 'file.txt' sizeObj['value'] = 12 this.properties.push(nameObj) this.properties.push(sizeObj) console.log(this.properties) } } })
After this change your code will work fine进行此更改后,您的代码将正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.