首先查看一下umi的版本,查看package.json的umi版本
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"umi": "^3.5.41"
},
"devDependencies": {
"mockjs": "^1.1.0"
}
2.引入mock.js的库,进行接口封装
import mockjs from "mockjs";
export default {
// 使用 mockjs 等三方库
"GET /api/tags": mockjs.mock({
"list|100": [{ name: "@city", "value|1-100": 50, "type|0-2": 1 }],
}),
};
3.使用fetch进行接口调用
let data = [];
let url = "http://localhost:8000/api/tags";
fetch(url)
.then((resolve) => {
if (resolve.ok) {
return resolve.json();
}
})
.then((resolve) => {
data = resolve.list;
// console.log(data);
});