[英]Vue.js - call API and render response from child single-file component
Vue.js version 2, with single-file components and vue-router (and webpack but that shouldn't be important here). Vue.js版本2,具有单个文件组件和vue-router(以及webpack,但这在这里并不重要)。
I've researched this as well as I believe I can and cannot seem to unravel a good pattern for populating and rendering an object when a component is rendered, hoping the solution is obvious to someone and easily explained. 我已经对此进行了充分的研究,我相信我可以也不能似乎无法阐明在渲染组件时用于填充和渲染对象的良好模式,希望解决方案对某人显而易见并易于解释。
Main.js (called from webpack.base.config): Main.js (从webpack.base.config中调用):
var vm = new Vue({
el: '#app',
router,
template: '<App/>',
components: { App }
})
App.vue: App.vue:
<template>
<router-view></router-view>
</template>
<script>
export default {
name: 'app'
}
</script>
Child.vue (it's important to note that what I'm doing here is 1) making an api call (via javascript promise) 2) iterating the response and populating the temp Object
let x = {}
properties with keys and values of each important bit then 3) triggering the computed.cObj.set()
in effort to render the list): Child.vue (需要注意的是,我在这里要做的是1)(通过javascript promise)进行api调用2)迭代响应并填充temp
Object
let x = {}
属性具有每个重要的键和值然后3)触发为computed.cObj.set()
列表而工作的computed.cObj.set()
):
<template>
<div class="child">
<ul>
<li v-for="i in cObj">{{ i }}</li>
</ul>
</div>
</template>
<script>
export default {
name: 'child',
data () {
return {
obj: {}
}
},
computed: {
cObj: {
get: function () {
return this.obj
},
set: function (nv) {
this.obj= nv
}
},
// cObj: function () {
// return this.getAll()
// }
},
created () {
let conditional = true
if (!conditional) // ...do something else
else this.getAllX()
},
methods: {
getAll: function () {
let x = {} // temporary object
// in this promise I'm returning data from the server
server.getData()
.then(function (r) {
// iterate response
r.records[0].operations().then(r => {
for (let a in r) {
if (typeof r[a].records === 'object') {
for (let b in r[a].records) {
Object.keys(r[a].records[b]).forEach(function (key) {
if (r[a].records[b][key] !== undefined) {
// add key/value to x
x[key] = r[a].records[b][key]
}
})
}
}
}
})
})
.catch(function (err) {
console.log(err)
})
this.cObj = x // THIS IS WHAT IS KILLING ME HERE, I must be misunderstanding the documentation here because the computed `set` method isn't triggered or I'm misunderstanding vue lifecycle, I thought calling the computed.cObj.set() should trigger the rendering of the list but it does not.
// return x | tried this as well (see commented out computed.cObj)
}
}
}
</script>
Looking at the populated object in console I get the following but the list isn't rendered: 在控制台中查看填充的对象,我得到以下内容,但未呈现该列表:
// {}
// detailOne: "asdf"
// detailTwo: "asdf asdf"
// __proto__: Object { … }
Here is a similar question Vue.js : How to make dynamic menu? 这是一个类似的问题Vue.js:如何制作动态菜单?
Try this in your Child.vue : 在您的Child.vue中尝试一下 :
The Array Way: 数组方式:
<template>
<div>
<ul>
<li v-for="(item, index) in cObj" :key="index">{{ item.name }}</li>
</ul>
</div>
</template>
<script>
export default {
data () {
return {
cObj: {}
}
},
created () {
let conditional = true
if (!conditional) {
}
else {
this.getAll()
}
},
methods: {
getAll: function () {
let x = [
{ id: 1, name: 'one' },
{ id: 2, name: 'two' },
{ id: 3, name: 'thre' },
]
this.cObj = x;
}
}
}
</script>
The Object Way: 对象方式:
<template>
<div>
<ul>
<li v-for="(value, key) in cObj" :key="key">{{ key }} : {{ value.name }}</li>
</ul>
</div>
</template>
<script>
export default {
data () {
return {
cObj: {}
}
},
created () {
let conditional = true
if (!conditional) {
}
else {
this.getAll()
}
},
methods: {
getAll: function () {
let x = {
first: { id: 1, name: 'one' },
second: { id: 2, name: 'two' },
third: { id: 3, name: 'three' },
};
this.cObj = x;
}
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.