当前位置 : 主页 > 网络编程 > 其它编程 >

vue使用mock

来源:互联网 收集:自由互联 发布时间:2023-07-02
安装npminstallmockjsmockjs原理拦截所有请求并代理到本地,然后进行数据模拟。使用mockjs出现的问题一些底层依赖XMLHttpRequest的库都会和 安装 npm install mockjs mockjs原理 拦截所有请求并代理到
安装npminstallmockjsmockjs原理拦截所有请求并代理到本地,然后进行数据模拟。使用mockjs出现的问题一些底层依赖XMLHttpRequest的库都会和

安装

npm install mockjs

mockjs原理

拦截所有请求并代理到本地,然后进行数据模拟。

使用mockjs出现的问题

一些底层依赖XMLHttpRequest的库都会和它发生不兼容,本地调试只能通过看源码或者Debug出来

解决方案

如果是本地运行则会启动mock-serve运行,线上环境还是继续使用mockjs来进行模拟。一套mock数据可以多环境使用

工程目录准备

├── mock // mock 模拟数据 与src同级│ ├── index.js // 数据总入口│ ├── mock-server.js // 数据服务层 │ ├── article.js // 数据块

开始

1、将本地的请求会代理到http://localhost:${port}/mock下。mock-server Node中间件拦截实现,注入方式

proxy: {[process.env.VUE_APP_BASE_API]: {target: `http://192.168.123.149:${port}/mock`,changeOrigin: true,pathRewrite: {['^' + process.env.VUE_APP_BASE_API]: ''}}},after: require('./mock/mock-server.js')

2、数据块article.js,实现对单组接口统一的定义

import Mock from 'mockjs'const List = []const count = 100const baseCOntent= '

I am testing data, I am testing data.

'const image_uri = 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3'for (let i = 0; i {const { importance, type, title, page = 1, limit = 20, sort } = config.querylet mockList = List.filter(item => {if (importance && item.importance !== +importance) return falseif (type && item.type !== type) return falseif (title && item.title.indexOf(title) index = limit * (page - 1))return {code: 20000,data: {total: mockList.length,items: pageList}}}},{url: '/article/detail',type: 'get',response: cOnfig=> {const { id } = config.queryfor (const article of List) {if (article.id === +id) {return {code: 20000,data: article}}}}},{url: '/article/pv',type: 'get',response: _ => {return {code: 20000,data: {pvData: [{ key: 'PC', pv: 1024 },{ key: 'mobile', pv: 1024 },{ key: 'ios', pv: 1024 },{ key: 'android', pv: 1024 }]}}}},{url: '/article/create',type: 'post',response: _ => {return {code: 20000,data: 'success'}}},{url: '/article/update',type: 'post',response: _ => {return {code: 20000,data: 'success'}}}]

3、index.js 模拟数据的导入导出,路由批量注册,以及公用逻辑实现,同时兼容了Mock和中间件的使用

import Mock from 'mockjs'import { param2Obj } from '../src/utils'import article from './article'const mocks = [...article]export function mockXHR() {Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.sendMock.XHR.prototype.send = function() {if (this.custom.xhr) {this.custom.xhr.withCredentials = this.withCredentials || falseif (this.responseType) {this.custom.xhr.respOnseType= this.responseType}}this.proxy_send(...arguments)}function XHR2ExpressReqWrap(respond) {return function(options) {let result = nullif (respond instanceof Function) {const { body, type, url } = optiOnsresult= respond({method: type,body: JSON.parse(body),query: param2Obj(url)})} else {result = respond}return Mock.mock(result)}}for (const i of mocks) {Mock.mock(new RegExp(i.url), i.type || 'get', XHR2ExpressReqWrap(i.response))}}const respOnseFake= (url, type, respond) => {return {url: new RegExp(`/mock${url}`),type: type || 'get',response(req, res) {res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond))}}}export default mocks.map(route => {return responseFake(route.url, route.type, route.response)})

4、mock-serve中间件逻辑实现分两部分进行讲述,第一节是路由的批量注册,第二节是文件监听实现自动重启服务

// 监听文件或文件夹的变化const chokidar = require('chokidar')// 处理程序之前,在中间件中对传入的请求体进行解析const bodyParser = require('body-parser')// 输出不再单调,添加文字背景const chalk = require('chalk')// 处理文件路径const path = require('path')const mockDir = path.join(process.cwd(), 'mock')function registerRoutes(app) {let mockLastIndexconst { default: mocks } = require('./index.js')for (const mock of mocks) {app[mock.type](mock.url, mock.response)mockLastIndex = app._router.stack.length}const mockRoutesLength = Object.keys(mocks).lengthreturn {mockRoutesLength: mockRoutesLength,mockStartIndex: mockLastIndex - mockRoutesLength}}function unregisterRoutes() {Object.keys(require.cache).forEach(i => {if (i.includes(mockDir)) {delete require.cache[require.resolve(i)]}})}// 请求→响应头解析→注册监听逻辑→清除缓存→响应服务module.exports = app => {require('@babel/register')app.use(bodyParser.json())app.use(bodyParser.urlencoded({extended: true}))const mockRoutes = registerRoutes(app)var mockRoutesLength = mockRoutes.mockRoutesLengthvar mockStartIndex = mockRoutes.mockStartIndex// watch files, hot reload mock serverchokidar.watch(mockDir, {ignored: /mock-server/,ignoreInitial: true}).on('all', (event, path) => {if (event === 'change' || event === 'add') {try {// remove mock routes stackapp._router.stack.splice(mockStartIndex, mockRoutesLength)// clear routes cacheunregisterRoutes()const mockRoutes = registerRoutes(app)mockRoutesLength = mockRoutes.mockRoutesLengthmockStartIndex = mockRoutes.mockStartIndexconsole.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed ${path}`))} catch (error) {console.log(chalk.redBright(error))}}})}

5、main.js 当前运行环境判定挂载mock对象(mock-server只会在线下开发环境中使用)

import { mockXHR } from '../mock'if (process.env.NODE_ENV === 'development') {mockXHR()}

参考链接 https://panjiachen.github.io/vue-element-admin-site/zh/guide/essentials/mock-api.html#swagger

【文章转自bgp服务器 http://www.558idc.com/yz.html提供,感恩】
上一篇:python之字典总结
下一篇:没有了
网友评论