最近工作不是很忙,自己做了一個vue的移動端的小項目,涉及到后台數據的時候,網上查閱了一些資料,因為是自己寫的項目沒有后台只能自己模擬數據,剛開始就自己寫了一些靜態數據后來覺得盡量模擬真實的比較好些,剛好也了解一下axios因為之前用的是vue-resource,就此開始挖坑填坑之路-------
首先說一下mock.js http://mockjs.com/這是它的官網,講的還是很詳細,主要有各種數據模板可以去定義,廢話不多說,下面開始正文
1、安裝mock
npm install mockjs --save-dev
2、安裝axios
npm install axios --save
3、我們來看一下項目結構
mock.js
1 const Mock = require('mockjs'); 2 // 獲取 mock.Random 對象 3 const Random = Mock.Random; 4 // mock一組數據 5 const userInfoData = function() { 6 let person = [ 7 { 8 label:'姓名', 9 text:Random.cname(),//隨機生成中文名 10 }, 11 { 12 label:'身份證號', 13 text:Random.id()//隨機生成身份證 14 }, 15 { 16 label:'手機號', 17 text:/^[1][3,4,5,7,8][0-9]{9}$/ //正則生成手機號 18 }, 19 { 20 label:'地址', 21 text:Random.county(true) //隨機生成地址 22 }, 23 { 24 label:'返佣銀行卡號', 25 text:/^([1-9]{1})(\d{14}|\d{18})$/ 26 }, 27 { 28 label:'開戶行', 29 text:Random.cword(2,4)+'銀行' 30 }, 31 ] 32 return { 33 person: person 34 } 35 } 36 37 // Mock.mock( url, post/get , 返回的數據); 38 Mock.mock('/user/index', 'post', userInfoData()); 39 40 41 42 Mock.mock('user/ma','post',{ 43 phone:/^[1][3,4,5,7,8][0-9]{9}$/, 44 photo:Random.dataImage('118x118', '居間協議') 45 }); 46 47 Mock.mock('user/login','post',{ 48 userId:'@natural(0,100)' 49 }) 50 51 Mock.setup({ 52 timeout:0-300 53 })
這個就是mock.js里面的數據,你可以在里面定義各種數據模板,然后當然還需要模擬的api這個就是在axios/api.js里面統一管理進行攔截ajax請求
axios/api.js如下
1 import axios from 'axios' 2 import vue from 'vue' 3 4 axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded' 5 6 // 請求攔截器 7 axios.interceptors.request.use(function(config) { 8 return config; 9 }, function(error) { 10 return Promise.reject(error); 11 }) 12 // 響應攔截器 13 axios.interceptors.response.use(function(response) { 14 return response; 15 }, function(error) { 16 return Promise.reject(error); 17 }) 18 19 // 封裝axios的post請求 20 export function fetch(url, params) { 21 return new Promise((resolve, reject) => { 22 axios.post(url, params) 23 .then(response => { 24 resolve(response.data); 25 }) 26 .catch((error) => { 27 reject(error); 28 }) 29 }) 30 } 31 32 export default { 33 JH_info(url, params) { 34 return fetch(url, params); 35 } 36 }
這里就自己看,不用多說了,最后用export default去暴露接口進行引用
接下來就是怎么在自己頁面調用mock數據
上代碼
1 import api from '../../axios/api.js' //引入api 2 export default { 3 4 data(){ 5 return { 6 proStatue:'等待上傳居間協議', 7 proPhoto:require('../../assets/img/logo.png'), 8 upload:'ok', 9 imageUrl:'', 10 user:[ 11 { 12 label:'姓名', 13 text:'小花' 14 }, 15 { 16 label:'身份證號', 17 text:'467829299277718369' 18 }, 19 { 20 label:'手機號', 21 text:'136890288802' 22 }, 23 { 24 label:'開啟時間', 25 text:'2018-05-01' 26 }, 27 { 28 label:'來源', 29 text:'都某人' 30 }, 31 { 32 label:'權益', 33 text:1000.00 34 }, 35 { 36 label:'手續費', 37 text:1000.00 38 }, 39 40 ], 41 } 42 }, 43 created(){ 44 this.setUserApi();//調用了定義的請求方法 45 api.JH_info('user/ma','123')//直接頁面創建的時候請求接口 46 .then(res=>{ 47 console.log('img',res); 48 this.imageUrl = res.photo; 49 }) 50 }, 51 methods:{ 52 // 圖片上傳 53 handleAvatarSuccess(res, file) { 54 console.log('file',file); 55 this.imageUrl = URL.createObjectURL(file.raw); 56 }, 57 beforeAvatarUpload(file) { 58 const isJPG = file.type === 'image/jpeg'; 59 const isLt2M = file.size / 1024 / 1024 < 2; 60 61 if (!isJPG) { 62 this.$message.error('上傳頭像圖片只能是 JPG 格式!'); 63 } 64 if (!isLt2M) { 65 this.$message.error('上傳頭像圖片大小不能超過 2MB!'); 66 } 67 return isJPG && isLt2M; 68 }, 69 //提交審核 70 checkInfo(){ 71 this.upload = 'true'; 72 this.proStatue = '等待審核'; 73 }, 74 75 setUserApi(){ 76 api.JH_info('/user/index','123') 77 .then(res => { 78 console.log(res); 79 this.user = res.person; 80 }); 81 }, 82 83 } 84 }
這是控制台打印的數據
當然還有更多mock的數據模板你可以去嘗試