日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

vite-plugin-mock使用方式

作者:程序員小易 更新時間: 2021-12-11 編程語言
yarn add vite-plugin-mock mockjs -D
	//vite.config.ts
  export default ({ command, mode }: ConfigEnv): UserConfig => {
  //command 可以根據'build' | 'serve'不同環境來配置
  const isBuild = command === 'build'
  //mode拿環境變量 development開發 |production 生產
  const root = process.cwd()
  const env = loadEnv(mode, root)
  return {
    plugins:[ viteMockServe({
    mockPath: 'mock',
    localEnabled: !isBuild
  })]
  }
}

//ts.config.json
"include": ["mock/**/*.ts"]

根目錄創建mock文件夾

import { Random } from 'mockjs'
import { MockMethod } from 'vite-plugin-mock'
export default [
  {
    url: '/api/info',
    method: 'get',
    response: () => {
        return {
          code: 200,
          message: '請示成功',
          type: 'success',
          data: {
            name: 'yyyy',
            age: 18,
            avatar: '/avatar/xj.jpg'
          }
        }
      }
  }
 
] as MockMethod[]

使用方式


let data = await fetch('api/info', {
  method: 'get'
}).then(res => res.json())
console.log(data)

原文鏈接:https://blog.csdn.net/yshijb/article/details/122113813

欄目分類
最近更新