转自:https://blog.csdn.net/weixin_43452622/article/details/86606196
service层
service层主要是新建一个js文件,用来写用到的方法。
import axios from "axios";
//使用假数据
const isRealData = require("../../static/serverconfig").isRealData;
var EquipmentService = {
getVenders: function () {
return new Promise((resolve, reject) => {
//使用真数据
if (isRealData) {
axios .get("/equipment-venders").then(res => {
let {code, data, msg} = res.data;
if (code == 200) {
resolve(data);
} else {
reject(msg + ",地址:/venders");
}
})
.catch(error => {
reject(error.response.status + " " + error.response.data);
});
} else {
// 假数据
}
});
},
}
export {
EquipmentService
};
在要使用的文件中引入service层
<script>
import { EquipmentService } from "../../../../services/equipmentService";
</script>
export default {
mounted(){
//获取供应商
EquipmentService.getVenders().then(
res=>{
this.venders = res
},
error=>{
this.Log.info(error)
}
)
}
}