国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

微信小程序結(jié)合mock.js實(shí)現(xiàn)后臺(tái)模擬及調(diào)試

2019-11-19 11:56:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、創(chuàng)建小程序項(xiàng)目

mock.js 從 https://github.com/nuysoft/Mock/blob/refactoring/dist/mock.js 下載

api.js:配置模擬數(shù)據(jù)和后臺(tái)接口數(shù)據(jù),通過(guò)DEBUG = true;//切換數(shù)據(jù)入口

let API_HOST = "http://xxx.com/xxx";let DEBUG = true;//切換數(shù)據(jù)入口var Mock = require('mock.js')function ajax(data = '', fn, method = "get", header = {}) {  if (!DEBUG) {    wx.request({      url: config.API_HOST + data,      method: method ? method : 'get',      data: {},      header: header ? header : { "Content-Type": "application/json" },      success: function (res) {        fn(res);      }    });  } else {    // 模擬數(shù)據(jù)    var res = Mock.mock({      'error_code': '',      'error_msg': '',      'data|10': [{        'id|+1': 1,        'img': "@image('200x100', '#4A7BF7','#fff','pic')",        'title': '@ctitle(3,8)',        'city': "@county(true)",        'stock_num': '@integer(0,100)',//庫(kù)存數(shù)量         'marketing_start': '@datetime()',        'marketing_stop': '@now()',        'price': '@integer(100,2000)',//現(xiàn)價(jià),單位:分         'original_price': '@integer(100,3000)'      }]     })    // 輸出結(jié)果    // console.log(JSON.stringify(res, null, 2))    fn(res);  }}module.exports = {  ajax: ajax}

index.js頁(yè)面

//index.js//獲取應(yīng)用實(shí)例var app = getApp()var API = require('../../utils/api.js')Page({  data: {  },  onLoad: function () {    console.log('onLoad')    var that = this    // 使用 Mock    API.ajax('', function (res) {      //這里既可以獲取模擬的res      console.log(res)      that.setData({        list:res.data      })    });    console.log(this.data.list)  }})

index.wxml

<!--index.wxml--><block wx:for="{{list}}" wx:key="name"> <view>{{item.title}}</view> <text>{{item.city}}</text> <view>  <text>{{item.marketing_start}}</text> </view> <image src='{{item.img}}'></image></block>

頁(yè)面顯示

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿勒泰市| 平利县| 辉县市| 成武县| 柏乡县| 海淀区| 兴海县| 积石山| 抚松县| 温宿县| 准格尔旗| 当雄县| 根河市| 温州市| 鄱阳县| 布拖县| 灵川县| 武穴市| 马鞍山市| 雷州市| 三河市| 赣榆县| 三门峡市| 新巴尔虎左旗| 于都县| 黎城县| 义马市| 井研县| 洛川县| 河西区| 伊春市| 闵行区| 如皋市| 阿图什市| 寻乌县| 阿克陶县| 巧家县| 镶黄旗| 新民市| 桃园市| 阿勒泰市|