vue axios二次封裝返回promise 調用時如何讀取

一直傻了沒懂then調用什麼意思,其實是在調用接口的地方使用then。
下面各自截取部分來講述。

首先我在http.js裏面對axios進行了封裝。

// 響應攔截器
var instance = axios.create({    timeout: 1000 * 12});
instance.interceptors.response.use(
    // 請求成功
    res => res.status === 200 ? Promise.resolve(res) : 
    });

然後把全部調用都寫在了index.js裏面。

const order = {
    Get_Order(userId){
        return axios.post(`${base.order}/GetOrderByInfo/`,
            {'userId':userId}
        );
    }
};

main.js全局註冊掛載。

import api from './api'
Vue.prototype.$api = api; // 將api掛載到vue的原型上

然後組件裏在methods中調用接口,加上then就可以獲取promise裏面的value。

                this.$api.order.Get_Order(userId)
                    .then((res) => {
                            console.log(res.data);
                            if (res.data.result !== "fail") {
                                console.log(res.data.orders);
                            }
                        }
                    );
            },
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章