Mock.Random 是一个工具类,用于生成各种随机数据,使得数据更加的真实。
Mock.Random 类中的方法在数据模板中称为『占位符』,书写格式为 '属性名': @占位符
Type(类型) | Method(占位符) |
Basic(基本类型) | boolean , natural (自然数,大于等于 0 的整数), integer , float , character , string , range (整型数组), |
Date(日期) | date (年月日), time (时分秒), datetime (年月日时分秒) |
Image(图片) | image, dataImage |
Color | color |
Text(文本) | paragraph, sentence, word, title(标题), cparagraph, csentence(内容), cword, ctitle |
Name(姓名) | first(英文姓), last(英文名), name(英文姓名), cfirst(中), clast, cname |
Web | url, domain, email, ip, tld |
Address(地址) | area, region |
Helper(不常用) | capitalize, upper, lower, pick, shuffle |
Miscellaneous(不常用) | guid, id |
练习代码:
const Mock=require('mockjs')
const data=Mock.mock({
'empList|6':[
{
'id|+1':1,
'name': '@cname', //中文名称
'price':'@float',
'status':'@boolean',
'birthday':'@date("yyyy/MM/dd")',
'createDate':'@datetime("yyyy/MM/dd HH-mm-ss" )',
'pic':'@image',
'title':'@title',
'ctitle':'@ctitle(10,16)',
'content':'@csentence(100,200)',
'first':'@cfirst', //姓
'last':'@clast', //名
'url':'@url("https","cc.com")',
'domain':'@domain',
'ip':'@ip',
'area':'@region',
'address':'@county(true)',
'zip':'@zip',
'phone|11':'@integer(0,9)' //数据模板和占位符结合使用
}
]
})
console.log(JSON.stringify(data,null,2))