简体   繁体   English

异步等待在 vue.js 中返回 [object Promise]

[英]async await returning [object Promise] in vue.js

I' using async function like this:我像这样使用异步 function :

methods:{
    async getUser(id)
    { 
 
      const header = '........';
        let responses=await axios.get(`/api/backend/method/${id}` , {headers: header});
      return responses.data.name; 

    },

calling it on load of td in a loop, like this像这样在循环加载 td 时调用它

<td v-html="getUser(request.id)"></td>

now in my td i get [object Promise] but in this variable responses.data.name there is name, i want to return that name.现在在我的 td 我得到[object Promise]但是在这个变量responses.data.name中有名字,我想返回那个名字。 Any help is highly appreciated.非常感谢任何帮助。

data() {
    return {
    // .... other vars
    usernames: {},
    requests: [],
    };
},
methods:{
    afterRequestLoaded() {
        this.requests.forEach((request) => this.loadusername(request.id);
    },
    async loadusername(id)
    { 
 
      const header = '........';
        let responses=await axios.get(`/api/backend/method/${id}` , {headers: header});
        this.usernames[id] = responses.data.name; 
    },
<tr v-for="request in requests">
   <td v-text="usernames[request.id]"></td>
</tr>

Note that afterRequestLoaded function should be executed when requests array is populated.注意afterRequestLoaded function 应该在requests数组被填充时执行。 perhaps by another api call.也许通过另一个 api 电话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM