我用这种方式,但不渲染
this.res = Object.assign({},
this.res, {
"employees": [{
"firstName": "John",
"lastName": "Doe"
}, {
"firstName": "Anna",
"lastName": "Smith"
}, {
"firstName": "Peter",
"lastName": "Jones"
}],
'haha': 'baba',
'papa': {
'mimi': 'mimi'
}
})
答案 0 :(得分:1)
不是在get方法中指定对象,而是在创建的生命挂钩中执行它,因此res属性将在呈现页面之前填充数据
var vue = new Vue({
el: "#app",
data: function() {
return {
res:{}
}
},
created: function() {
this.res = {
"employees": [{
"firstName": "John",
"lastName": "Doe"
}, {
"firstName": "Anna",
"lastName": "Smith"
}, {
"firstName": "Peter",
"lastName": "Jones"
}],
'haha': 'baba',
'papa': {
'mimi': 'mimi'
}
}
}
})
<script src="https://vuejs.org/js/vue.min.js"></script>
<div id="app" >
<div>{{res.employees[0].firstName}}</div>
<div>{{ res.papa.mimi}}</div>
<div>{{ res.haha}}</div>
</div>
答案 1 :(得分:0)
employees
未在渲染时定义(创建)。
您应该直接在数据中写入res对象。