【发布时间】:2017-08-13 05:17:24
【问题描述】:
告诉我如何从嵌入的 json 中提取数据。 Vuejs 中的数据是从数据库中传输过来的。 Google Chrome 的调试器中有数据。数据库中存在一对多关系。一种配方,多种成分
示例 Vue
var vm = new Vue({
el: '#my_view',
data: {
recipeslist: [],
recipes: {}
},
created: function () {
this.$http.get('/recipe').then(function (data) {
this.$set(this, 'recipeslist', data.body.items)
$( "#recipe_view" ).hide();
})
},
methods: {
showrecipe: function (event) {
this.$http.get('/recipe/'+event.target.id).then(function (data) {
this.$set(this, 'recipes', data.body.items)
$( "#recipe_view" ).show();
$( "#main_view" ).hide();
})
}
}
})
示例 html
<div class="root"v-for="index in recipes">
<h6> {{index.recipename}} </h6>
<h6> {{index.flavorid}} </h6>
</div>
HTML 视图
test 1
3
test 1
4
使用此代码的示例数据
<div class="root"v-for="index in recipes">
<h6> {{index}} </h6>
</div>
{ "id": 1, "bottleID": 1, "recipename": "test 1", "bottlesize": 60, "date": 1, "flavorslist": 1, "note": "none", "flavorid": 3, "NAME": "tpa lime", "drops": 5, "recepeid": 1 }
{ "id": 1, "bottleID": 1, "recipename": "test 1", "bottlesize": 60, "date": 1, "flavorslist": 1, "note": "none", "flavorid": 4, "NAME": "tpa prome", "drops": 1, "recepeid": 1 }
试过所以写了一个错误:vue.min.js:6 TypeError: Cannot read property 'recipename' of undefined
<div class="root"v-for="index in recipes">
<h6> {{index[0].recipename}} </h6>
<h6> {{index[0].flavorid}} </h6>
</div>
一般来说,我需要得到:
一份recipename、bottleID、bottlesize、date(因为这是recipe的基础,在json中也是一样的)但是一些NAME,drops(因为这些是成分,可能有几个)
【问题讨论】: