mock.js簡(jiǎn)介
? 官方鏈接:Mock.js (mockjs.com)
????????前端開發(fā)人員用來模擬虛擬數(shù)據(jù),攔截ajax請(qǐng)求,方便模擬后端接口
安裝
npm install mockjs
使用
????????本文主要介紹在Vue項(xiàng)目中使用mock.js,包括axios發(fā)送請(qǐng)求與請(qǐng)求簡(jiǎn)單封裝
-
創(chuàng)建mock文件夾,新建index.js文件
// 引入mockjs import Mock from "mockjs"; // 獲取 mock.Random 對(duì)象 const Random = Mock.Random; // 使用mockjs模擬數(shù)據(jù) let tableList = [ { id: "5ffa80aD-9CF4-0C77-eBFC-f6612BfAcF4F", account: "admin", password: "123456", address: "36918166@qq.com", }, { id: "4FcC922C-C72c-95c3-Ef92-FbFAc24cc831", account: "ebHoL6", password: "i320Hu74fbn2Gi", address: "48165263@qq.com", }, ] // for (let i = 0; i < 20; i++) { // let newObject = { // id: Random.guid(), // 獲取全局唯一標(biāo)識(shí)符 // account: /^[a-zA-Z0-9]{4,6}$/, // password: /^[a-zA-Z]\w{5,17}$/, // address: /[1-9]\d{7,10}@qq\.com/, // }; // tableList.push(newObject); // } /** get請(qǐng)求 * 獲取用戶列表 */ Mock.mock("/api/mockGetList", "get", () => { return { code: "0", data: tableList, }; }); /** post請(qǐng)求添加表格數(shù)據(jù) */ Mock.mock("/api/add", "post", (params) => { let newData = JSON.parse(params.body); newData.id = Random.guid(); tableList.push(newData); return { code: "0", message: "success", data: tableList, }; });
? ? ? ? 模擬數(shù)據(jù)可自己手動(dòng)編寫,也可由for循環(huán)自動(dòng)生成,可以設(shè)置數(shù)量,字段(可以通過正則表達(dá)式限制輸出格式)。最后可設(shè)定請(qǐng)求路徑,請(qǐng)求方式以及返回內(nèi)容,可根據(jù)自身需求進(jìn)行更改。
-
創(chuàng)建api文件夾,新建http.js文件(請(qǐng)求封裝)
import axios from "axios"; import { ElLoading, ElMessage } from "element-plus"; let http = axios.create({ baseURL: "", timeout: 10000, }); let loadingInstance; // 攔截器的添加 http.interceptors.request.use( (config) => { loadingInstance = ElLoading.service("加載中"); return config; }, (err) => { loadingInstance?.close(); ElMessage.error("網(wǎng)絡(luò)異常"); return Promise.reject(err); } ); //響應(yīng)攔截器 http.interceptors.response.use( (res) => { loadingInstance?.close(); return res.data; }, (err) => { loadingInstance?.close(); ElMessage.error("請(qǐng)求失敗"); return Promise.reject(err); } ); export default http;
這部分主要是對(duì)請(qǐng)求進(jìn)行封裝
-
新建mockApi.js文件(接口封裝)
import http from "./http.js"; export default { //用戶列表 findAll() { return http({ url: `/api/mockGetList`, method: "get", }); }, //添加用戶 addUser(user) { return http({ url: `/api/add`, method: "post", data: user, }); }, }
注意:url與提交方法要與mock中模擬請(qǐng)求保持一致
-
調(diào)用封裝好的接口
?????????導(dǎo)入模擬數(shù)據(jù)與接口文件,根據(jù)自己的路徑進(jìn)行修改
import "../mock/index.js";
import mockApi from "../api/mockApi/mockApi.js";
????????調(diào)用接口
//頁面數(shù)據(jù)請(qǐng)求
let tableData = reactive([]);
const getList = () => {
mockApi
.findAll()
.then((res) => {
console.log(res)
if (res.code === "0"){
tableData.push.apply(tableData, res.data);
}
})
.catch(function (error) {
console.log(error);
});
};
getList(); //直接調(diào)用請(qǐng)求方法
//添加用戶
mockApi
.addUser(editUser)
.then((res) => {
console.log(res)
if (res.code === "0") {
ElMessage({
message: "保存成功",
type: "success",
});
}
})
.catch(function (error) {
console.log(error);
});
項(xiàng)目結(jié)構(gòu)
?結(jié)構(gòu)大體如上,mock中的Management.js就是文中說到的使用第一步,根據(jù)自身需要進(jìn)行修改文章來源:http://www.zghlxwxcb.cn/news/detail-433474.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-433474.html
到了這里,關(guān)于Vue3中簡(jiǎn)單使用Mock.js的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!