一直傻了沒懂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);
}
}
);
},