mock

作者: 43e1f527c136 | 来源:发表于2023-03-14 19:07 被阅读0次

    安装mockjs

    npm i mockjs --save

    写mock接口数据

    import Mock from 'mockjs'

    // 引入 json数据

    import analysis from './analysis.json'

    Mock.mock('/mock/analysis', {

        code:200,

        data: analysis

      })

    //mock请求封装

    安装axios

    import axios from 'axios'

    export const request = (options) => {

      return new Promise((resolve, reject) => {

        const service = axios.create({

          baseURL: '/mock',

          timeout: 2000, // request timeout

        })

        service.interceptors.request.use((config) => {

          return config

        })

        service.interceptors.response.use(

          (response) => {

            return response.data

          },

          (error) => {

            return Promise.reject(error)

          },

        )

        // 请求处理

        service(options)

          .then((res) => {

            resolve(res)

          })

          .catch((error) => {

            reject(error)

          })

      })

    }

    export default request

    //请求接口封装

    import mocks from '../util/mock'

    export const Analysis=()=> mocks('/analysis','get')

    相关文章

      网友评论

          本文标题:mock

          本文链接:https://www.haomeiwen.com/subject/xggcrdtx.html