小程序学习4 mock

services/home.js

javascript 复制代码
import { config, cdnBase } from '../../config/index';

/** 获取首页数据 */
function mockFetchHome() {
  const { delay } = require('../_utils/delay');
  const { genSwiperImageList } = require('../../model/swiper');
  return delay().then(() => {
    return {
      swiper: genSwiperImageList(),
      tabList: [
        {
          text: '精选推荐',
          key: 0,
        },

      ],
      activityImg: `${cdnBase}/activity/banner.png`,
    };
  });
}

/** 获取首页数据 */
export function fetchHome() {
  if (config.useMock) {
    return mockFetchHome();
  }
  return new Promise((resolve) => {
    resolve('real api');
  });
}
function mockFetchHome(){

这段代码是一个用于获取首页数据的函数fetchHome()。根据配置项config.useMock的值,函数会选择使用模拟数据(mockFetchHome())或者真实的API请求('real api')。

如果config.useMock为true,函数会调用mockFetchHome()函数来获取模拟数据。mockFetchHome()函数内部使用了延迟函数delay()来模拟网络请求的延迟。然后返回一个包含轮播图数据、选项卡列表和活动图片链接的对象。

如果config.useMock为false,函数会返回一个Promise对象,该Promise对象会立即resolve为'real api',表示使用真实的API请求。

相关推荐
如果你想拥有什么先让自己配得上拥有34 分钟前
近似数的思考学习
学习
hugerat40 分钟前
在AI的帮助下,用C++构造微型http server
linux·c++·人工智能·http·嵌入式·嵌入式linux
ha20428941941 小时前
Linux操作系统学习记录之----自定义协议(网络计算器)
linux·网络·学习
想唱rap2 小时前
MYSQL在ubuntu下的安装
linux·数据库·mysql·ubuntu
振华说技能2 小时前
SolidWorks学习大纲-从基础到全面精通,请看详情
学习
曦月逸霜2 小时前
离散数学-学习笔记(持续更新中~)
笔记·学习·离散数学
糖~醋排骨2 小时前
DHCP服务的搭建
linux·服务器·网络
im_AMBER2 小时前
Leetcode 101 对链表进行插入排序
数据结构·笔记·学习·算法·leetcode·排序算法
dust_and_stars2 小时前
ubuntu24使用apt安装VS-code-server code-server
linux·服务器·windows
Mr -老鬼2 小时前
Rust与Go:从学习到实战的全方位对比
学习·golang·rust