一、為什么要使用mockjs
總結起來就是在后端接口沒有開發完成之前,前端可以用已有的接口文檔,在真實的請求上攔截ajax,並根據mockjs的mock數據的規則,模擬真實接口返回的數據,並將隨機的模擬數據返回參與相應的數據交互處理,這樣真正實現了前后台的分離開發。
二、在vue的項目中怎么去使用mockjs
1、下載mockjs
npm install mockjs --save
2、使用mockjs
2.1在項目目錄中新建mock/mockServer.js 模擬服務端

1 import Mock from 'mockjs' 2 const swipes = [ 3 { 4 imgUrl:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556531040198&di=c6dabc6bb5c6524f9b77ceded00d1fce&imgtype=0&src=http%3A%2F%2Fi3.hexun.com%2F2019-04-28%2F196992413.jpg" 5 }, 6 { 7 imgUrl:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556531040180&di=0feae9ec159834591880d72c34137235&imgtype=0&src=http%3A%2F%2Fm1080.com%2Fupimg%2Fzyzp1%2F145186.jpg" 8 }, 9 { 10 imgUrl:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556531040164&di=b228224bb92c8baa6fbfa1ac6d31398c&imgtype=0&src=http%3A%2F%2Fimg.smzy.com%2Fimges%2F2017%2F0510%2F20170510101016609.jpg" 11 } 12 ]; 13 const patients =[ 14 { 15 id:'1', 16 title:'張大爺', 17 label:'男', 18 path:'/patient', 19 imgUrl:'../../static/images/20170622131955_h4eZS.thumb.700_0.jpeg' 20 }, 21 { 22 id:'2', 23 title:'李大爺', 24 label:'男', 25 path:'/patient', 26 imgUrl:'../../static/images/20170622131955_h4eZS.thumb.700_0.jpeg' 27 }, 28 { 29 id:'3', 30 title:'張奶奶', 31 label:'女', 32 path:'/patient', 33 imgUrl:'../../static/images/20170622131955_h4eZS.thumb.700_0.jpeg' 34 }, 35 { 36 id:'4', 37 title:'李大爺', 38 label:'男', 39 path:'/patient', 40 imgUrl:'../../static/images/20170622131955_h4eZS.thumb.700_0.jpeg' 41 }, 42 { 43 id:'5', 44 title:'王奶奶', 45 label:'女', 46 path:'/patient', 47 imgUrl:'../../static/images/20170622131955_h4eZS.thumb.700_0.jpeg' 48 } 49 ]; 50 Mock.mock('/swipes',swipes); 51 Mock.mock('/patients',patients); 52 Mock.mock("/patient", "post", (options)=>{ 53 const jsonObj = eval('(' + options.body + ')'); 54 const patient = patients.filter(p=>p.id == jsonObj.pid); 55 return patient[0]; 56 });
2.2在main.js項目全局文件中引入mockServer
1 import './mock/mockServer' //加載mockServer
2.3前端發送ajax去請求mockServer 的 數據

1 /* 2 ajax請求函數模塊 3 返回值: promise對象(異步返回的數據是: response.data) 4 */ 5 import axios from 'axios' 6 export default function ajax (url, data={}, type='GET') { 7 8 return new Promise(function (resolve, reject) { 9 // 執行異步ajax請求 10 let promise 11 if (type === 'GET') { 12 // 准備url query參數數據 13 let dataStr = '' //數據拼接字符串 14 Object.keys(data).forEach(key => { 15 dataStr += key + '=' + data[key] + '&' 16 }) 17 if (dataStr !== '') { 18 dataStr = dataStr.substring(0, dataStr.lastIndexOf('&')) 19 url = url + '?' + dataStr 20 } 21 // 發送get請求 22 promise = axios.get(url) 23 } else { 24 // 發送post請求 25 promise = axios.post(url, data) 26 } 27 promise.then(function (response) { 28 // 成功了調用resolve() 29 resolve(response.data) 30 }).catch(function (error) { 31 //失敗了調用reject() 32 reject(error) 33 }) 34 }) 35 }

1 /* 2 包含n個接口請求函數的模塊 3 函數的返回值: promise對象 4 */ 5 import ajax from './ajax'; 6 7 //1、獲取swipe的圖片列表 8 export const reqSwipes = ()=>ajax(`/swipes`,); 9 10 //2、獲取所有病人信息列表 11 export const reqPatients = ()=>ajax(`/patients`); 12 13 //3、根據pid獲取病人信息 14 export const reqPatientByPid = (pid)=>ajax(`/patient`,{pid},"POST");