vue

vue中使用mockjs

Posted by Ethan on 2020-09-30

安装mockjs

1
npm install mockjs --save-dev

在src下创建一个mock的文件夹,里边存在index.js,这个文件是保存模拟数据路径的地方

1
2
3
4
import Mock from 'mockjs'
import order from './order'

Mock.mock(/\/order\/getOrderList/, 'get', order.getOrderList)

index.js

创建一个文件夹里边存储的是需要模拟的数据

index.js

src有一个专门放置访问接口的文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
import request from '@/utils/request'

/**
* @description: 查询订单列表
* @param {type}
* @return {type}
*/
export function getOrderList() {
return request({
url: '/order/getOrderList',
method: 'get'
})
}

index.js

在main.js文件中引用index.js

1
import './mock/index.js'

index.js

需要调用使用的接口

1
import { getOrderList } from '@/api/order/order.js'

index.js