MockJs基本语法
mockjs基本语法
属性值是字符串-String
'name|count': string
重复次数count'name|min-max': string
重复次数随机(大于等于min,小于等于max)1
2'str|7': '以使其更易于阅读 7次。',
'str1|1-6': '以使其更易于阅读 1-6随机次。',
属性值是数字-Number
'name|+1': number
属性值自动+1,初始值为number'name|min-max': number
生成一个大于等于min、小于等于max的整数,属性值number只是用来确定类型'name|min-max.dmin-dmax': number
生成一个浮点数,整数部分大于等于min,小于等于max;小数部分保留dmin到dmax位'name|count.dmin-dmax': number
count 默认整数'name|min-max.count': number
count 默认保留位数1
2
3
4
5
6
7'number|+1': 2,
'number1|1-7': 2.1,
'float|1-8.1-9': 1,
'float1|88.1-19': 1,
'float2|1-8.3': 1,
'float3|99.3': 1,
属性值是布尔型-Boolean
'name|1': boolean
随机生成一个布尔值,true和false生成概率为1/2'name|min-max': value
随机生成一个布尔值,value 的概率为min/(min+max), !value 的概率为max/(min+max)1
2'bool|1': true,
'bool1|1-7': true,
属性值是对象-Object
'name|count': object
从属性值object中随机选取count个属性'name|min-max': object
从属性值object中随机选取min-max个属性1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19'obj|1': {
'id': 'aaa',
'id1': 'bbb',
'id2': 'ccc',
'id3': 'ddd',
},
'obj1|2': {
'id': 'aaa',
'id1': 'bbb',
'id2': 'ccc',
'id3': 'ddd',
},
'obj2|2-3': {
'id': 'aaa',
'id1': 'bbb',
'id2': 'ccc',
'id3': 'ddd',
'id4': 'eee',
},
属性值是数组-Array
'name|1': array
从array中随机选取一个值作为最终值'name|+1': array
从array中顺序选取一个值作为最终值'name|min-max': array
生成一个数组,数组内容重复次数在min-max之间'name|count': array
生成一个数组,数组内容重复次数为count1
2
3
4
5
6
7
8
9
10
11
12
13
14'arr|1': ['2', '3', '@string'],
'arr1|1': [{ name: 2 }, { name: 3 },],
'arr2|+1': ['2', '3'],
'arr3|2-9': [2, 3],
'list|10': [
{
'id|+1': 1,
'uid': '@guid',
'name': '@cname',
'age|18-35': 1,
'email': '@email',
'phone': /^1[3-9]\d{9}$/
}
],
属性值是正则表达式-RegExp
'name': regexp
根据正则表达式regexp反向生成可以匹配它的字符串。用于生成自定义格式的字符串。1
'Reg': /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
属性值是函数-Function
'name': function
执行函数function,取其返回值作为最终的属性值,函数的上下文为属性’name’所在的对象。1
2
3
4
5
6'func': function () {
return 'func'
},
'func1': function () {
return this.date
},
完整代码
1 |
|
生成文件
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Coder Long!
评论