1、ts中安装mock
npm install @types/mockjs -D
2、新建mock文件
// src文件夹下
mock
├─index.ts
├─data
| └table.ts
具体接口table.ts文件
import { Random } from 'mockjs'
interface ListType {
id: number,
name: string,
code: number,
province: string,
date: string
}
const dataList: Array<ListType> = [];
for (let i = 0; i < 100; i++) {
dataList.push({
id: i,
name: Random.cname(),
code: Random.integer(100000, 800000),
province: Random.province(),
date: Random.date(),
})
}
export default {
getList: (params: any) => {
const info = JSON.parse(params.body);
const [index, size, total] = [info.pageIndex - 1, info.pageSize, dataList.length]
const len: any = total / size
const totalPages: number = len - parseInt(len) >= 0 ? parseInt(len) + 1 : len
const newDataList: Array<ListType> = dataList.slice(index * size, (index + 1) * size)
return {
total: dataList.length,
data: newDataList,
totalPages: totalPages
}
},
update: (params: any) => {
const info = JSON.parse(params.body);
dataList[info.index] = info.data
return {
result: true,
data: 'success'
}
},
delete: (params: any) => {
const info = JSON.parse(params.body);
dataList.splice(info.index, 1);
return {
result: true,
data: 'success'
}
}
}
index.ts文件
import Mock from 'mockjs'
import table from './data/table'
Mock.mock('/table/getList', 'post', table.getList)
Mock.mock('/table/update', 'post', table.update)
Mock.mock('/table/delete', 'post', table.delete)
export default Mock
3、main.ts文件
if (process.env.NODE_ENV === 'development') {
require ('./mock/index.ts')
}
4、请求封装
import request from '@/utils/request'
// 表格
export function getTableList(params: any): Promise<any> {
return request({
url: '/table/getList',
method: 'post',
data: params
})
}
export function deleteTableList(params: any): Promise<any> {
return request({
url: '/table/delete',
method: 'post',
data: params
})
}
export function updateTableList(params: any): Promise<any> {
return request({
url: '/table/update',
method: 'post',
data: params
})
}
5、调用
import { getTableList, deleteTableList, updateTableList } from "@/api/api";
getTableList() {
let params = {
pageIndex: this.pageIndex,
pageSize: this.pageSize,
};
getTableList(params).then((res: any) => {
this.tableData = res.data;
this.total = res.total;
});
},
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 模板不会安装或需要功能定制以及二次开发?
发表评论
还没有评论,快来抢沙发吧!