前端项目学习记录3:mock接口

1.下载mock接口

pnpm i vite-plugin-mock

2.配置vite.config.ts

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path";
//引入svg需要用到的插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
//mock插件提供方法
import { viteMockServe } from 'vite-plugin-mock'
export default defineConfig(({ command }) => {
  return {
    plugins: [vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]'
      }),
      viteMockServe({
        localEnabled: command === 'serve',
      })
    ],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      }
    },
    css: { //scss全局变量的配置
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: `@import "./src/styles/variable.scss";`
        }
      }
    },
  }
})

        如果localEnabled爆红的话就把vite-plugin-mock的版本换成2.9.6

"vite-plugin-mock": "^2.9.6",

3.项目文件夹下创建mock文件夹

        文件名看情况

function createUserList() {
    return [
        {
            id: 1,
            name: '张三',
            age: 18,
            password: '123456'
        },
        {
            id: 2,
            name: '李四',
            age: 20,
            password: '123456'
        }
    ]
}

//对外暴露一个数组:数组里面包含两个接口
//登录假的接口
//获取用户信息的假的接口
export default [
    {
        url: '/api/user/login',
        method: 'post',
        response: ({ body }) => {
            const { username, password } = body
            const userList = createUserList()
            const user = userList.find(item => item.name === username && item.password === password)
            if (user) {
                return {
                    code: 200,
                    message: '登录成功',
                    data: user
                    
                }
                
            } else {
                return {
                    code: 400,
                    message: '用户名或密码错误',
                    data: null
                }
            }
        }
    }
]

相关推荐

  1. 前端项目学习记录3mock接口

    2024-05-01 17:10:01       33 阅读
  2. 前端项目学习记录4:1,2,3总结

    2024-05-01 17:10:01       29 阅读
  3. vue3 使用 mock 模拟服务器接口

    2024-05-01 17:10:01       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-01 17:10:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-01 17:10:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-01 17:10:01       82 阅读
  4. Python语言-面向对象

    2024-05-01 17:10:01       91 阅读

热门阅读

  1. ts封装浏览器indexedDB

    2024-05-01 17:10:01       32 阅读
  2. 【DevOps】Docker安装和使用示例

    2024-05-01 17:10:01       22 阅读
  3. 使用rust学习基本算法(四)

    2024-05-01 17:10:01       27 阅读
  4. GPT每日面试题-Typescript中type和interface的区别

    2024-05-01 17:10:01       36 阅读
  5. 如何在Linux服务器上安装Stable Diffusion WebUI

    2024-05-01 17:10:01       33 阅读
  6. 行业早报0501

    2024-05-01 17:10:01       34 阅读
  7. FlinkSQL 中lateral table

    2024-05-01 17:10:01       30 阅读
  8. springboot配置WebMvcConfigurationSupport

    2024-05-01 17:10:01       32 阅读
  9. leetcode_41.缺失的第一个正数

    2024-05-01 17:10:01       31 阅读
  10. 【Chrony】Docker中的精准时间同步的高效解决方案

    2024-05-01 17:10:01       33 阅读