美文网首页Vue学习笔记Vue.js
在同一个组件中要求所有API都得到数据

在同一个组件中要求所有API都得到数据

作者: fred_33c7 | 来源:发表于2020-03-23 17:37 被阅读0次

    有时候,我们需要在同一个组件或者一个方法中,使得好几个API都获得数据之后,再做下面的事情,就需要用到Promise.all()这个方法。
    在VUE中,我们调用API,一般都是用的axios这个包,axios是基于Promise编写的.
    例如有三个API:

    • getName
    • getAge
    • getGrade
    function buildAPI() {
        return [getName().then(res => {
            dosomthing()
        }),getAge().then(res=>{
            dosomthing()
        }),,getGrade().then(res=>{
            dosomthing()
        })]
    }
    let apiList= buildAPI()
    Promise.all(apiList).catch(e => {
        console.error(e);
    })
    

    相关文章

      网友评论

        本文标题:在同一个组件中要求所有API都得到数据

        本文链接:https://www.haomeiwen.com/subject/fbowyhtx.html