【发布时间】:2018-01-15 19:16:21
【问题描述】:
我在使用 VueJS、axios、Async/Await with Promise(??) 从 REST-API 检索数据时遇到问题。 我需要调用两个不同的 REST-API。一个是给我一个列表,我可以遍历它,这很好。
<template>
<div>
<div v-if="posts && posts.length">
<div v-for="post of posts">
{{post.name}}
<img :src="getUserPicturePath(post.name)" />
</div>
</div>
</div>
</template>
如您所见,在 v-for 之间的另一个是调用使用 axios 的方法。
<script>
import axios from 'axios'
export default {
data: () => {
posts: []
}
created() {
// a method to fill up the post-array
},
methods: {
async getUserPicturePath(name){
const response = await axios.get('linkToApi'+name)
console.dir(response.data.profilePicture.path)
return response.data.profilePicture.path
}
}
}
</script>
console.dir(response.data.profilePicture.path)- 部分给出了 profilePicture 的正确路径,但上面的 <template> 中的 <img :src="getUserPicturePath(post.name)" /> 写为 [Object Promise]。
任何建议我如何获得路径?
【问题讨论】:
-
我目前遇到与您完全相同的问题,代码非常相似,我的图像返回
src="[object Promise]",您是如何解决这个问题的?
标签: rest promise vue.js async-await axios