本篇是上一篇的另一种实现,框架更新后,使用vite+pinia,自然mock数据也要跟上。基于vite-plugin-mock实现开发。
步骤
1. 安装vite-plugin-mock包
2. 在vite.config.ts中添加插件
| import { viteMockServe } from 'vite-plugin-mock'
export default defineConfig(({ mode }: ConfigEnv) => { return { plugins: [ viteMockServe({ mockPath: 'src/mock', localEnabled: true, }), ], } })
|
3. 新建mock文件夹
mock/index.ts
中存放mock的API列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import { MockMethod } from 'vite-plugin-mock'
const mockList: MockMethod[] = [ { url: '/mock/menu', method: 'get', statusCode: 200, response: (opt) => { return { status: 200, msg: '登录成功', data: [ { path: '/dash3', title: 'dash3', component: 'BasicLayout', meta: { title: '动态管理3', icon: 'userm', hidden: false, level: 1 }, children: [ { title: 'dash3', path: '/dash3', component: 'test', fold: 'home' } ] } ] } } } ] export default mockList
|
4. 写请求
api/user.ts
| export const getUserRoutes = (params: any) => request.get({ url: '/mock/menu', params })
|
5. 开启