[前端] Vue+Mockjs模拟curd接口请求的示例详解

1945 0
Honkers 2022-10-21 16:10:26 | 显示全部楼层 |阅读模式
在前后端分离的项目中常常会遇到当前端页面开发完成
但是后端接口还没好,暂不支持联调的情况下,一般我们会用到mock数据
这边简单说一下最常见且经常会遇到的curd接口模拟
注:这边可以和后端先约定好接口路径以及入参返参的字段,避免二次修改
1.安装依赖,新建js文件,在文件中导入mock.js,模拟列表数据
  1. yarn add mockjs
  2. const Mock = require("mockjs")
  3. const list = []
  4. const length = 18
  5. for (let i = 0; i < length; i++) {
  6.     list.push(
  7.         Mock.mock({
  8.             id: '@id',
  9.             account: '@first',
  10.             name: '@name',
  11.             email: '@email',
  12.             mobile: '@phone',
  13.             sex: '@integer(0,1)',
  14.             type: "@integer(100,101)",
  15.             status: "@integer(0,1)",
  16.         })
  17.     )
  18. }
复制代码
2.查询列表接口模拟
  1. {
  2.         url: "/user/getPageList",
  3.         type: "post",
  4.         response: config => {
  5.             // 拿到入参
  6.             const {
  7.                 name,
  8.                 account,
  9.                 status,
  10.                 type,
  11.                 pageNum,
  12.                 pageSize,
  13.             } = config.body;
  14.             // 做一些查询条件的处理
  15.             const mockData = list.filter(item => {
  16.                 if (name && item.name.indexOf(name) < 0) return false
  17.                 if (account && item.account.toString() !== account) return false
  18.                 if (status && item.status.toString() !== status) return false
  19.                 if (type && item.type.toString() !== type) return false
  20.                 return true
  21.             })
  22.             // 模拟分页
  23.             const pageList = mockData.slice((pageNum - 1) * pageSize, pageNum * pageSize)
  24.             // 返回数据
  25.             return {
  26.                 resultCode: "1",
  27.                 messageCode: null,
  28.                 message: null,
  29.                 data: {
  30.                     list: pageList,
  31.                     total: mockData.length
  32.                 }
  33.             };
  34.         }
  35.     },
复制代码
3.删除功能接口模拟
  1. {
  2.         url: "/user/removeRow",
  3.         type: "post",
  4.         response: config => {
  5.             const {
  6.                 id
  7.             } = config.body
  8.             // 根据id找到需要删除的元素索引
  9.             const index = list.findIndex(item => item.id === id)
  10.             // 调用splice删除
  11.             list.splice(index, 1)
  12.             return {
  13.                 resultCode: "1",
  14.                 messageCode: null,
  15.                 message: null,
  16.                 data: 'success'
  17.             }
  18.         }
  19.     },
复制代码
4.保存及编辑接口模拟
  1. {
  2.         url: "/user/saveForm",
  3.         type: "post",
  4.         response: config => {
  5.             const {
  6.                 id
  7.             } = config.body
  8.             if (id) {
  9.                 // 关键在于id,其他入参不多赘述,格局id找到那条数据调用splice替换
  10.                 const index = list.findIndex(item => item.id === id)
  11.                 list.splice(index, 1, config.body)
  12.             } else {
  13.                 // 如果id不存在则在列表添加一条数据
  14.                 list.unshift(
  15.                     Mock.mock({
  16.                         id: '@id',
  17.                         ...config.body
  18.                     })
  19.                 )
  20.             }
  21.             return {
  22.                 resultCode: "1",
  23.                 messageCode: null,
  24.                 message: null,
  25.                 data: 'success'
  26.             }
  27.         }
  28.     },
复制代码
如上便是简易的curd接口模拟,具体mock-server.js的配置可去网上查阅
所有接口使用module.exports导出后,在调用时就会执行mock的接口
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4008
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行