MOCK.js的用法

初用mock的体会

先上代码,如下,模拟数据的js。mock可以模拟目前业务所需的所有数据,属性key/生成规则rules/属性值value,还有一些指定类型数据的随机生成函数@cname @dreess等等 ,对用javascript开发客户端的人员来说,完全解决了模拟数据的问题,还是很不错的。使用也很方便。

...
		var Mock = require('../../utils/mock.js')
		Mock.mock('http://uiui.com',{
			"dataArry|50":[
				{
				'name|3':'fei',//这个定义数据的模板形式下面会介绍
				'age|20-30':25,
				'job':'program',
				 "shopAddress": "@county(true)", //随机生成地址
				 "shopMsg": "@ctitle(10)", //生成商品信息,长度为10个汉字
				 "shopName": "@cname",//生成商品名 , 都是中国人的名字
				 "shopTel": /^1(5|3|7|8)[0-9]{9}$/,//生成随机电话号
				 "shopAddress": "@county(true)", //随机生成地址
				 "shopStar|1-5": "★", //随机生成1-5个星星
				 "salesVolume|30-1000": 30, //随机生成商品价格 在30-1000之间
				 "shopLogo": "@Image('100x40','#c33', '#ffffff','艺术')", //生成随机图片,大小/背景色/字体颜色/文字信息
				}
			]
		})
	        uni.request({
		    url: 'http://uiui.com', 
			method:"get",
		    success: (res) => {
		        console.log(res.data);
		    },
			fail:(error)=>{
				console.log(error);
			}
		})

...

原文地址:https://www.cnblogs.com/aryu/p/12752447.html