Mock.Random

Mock.Random

Mock.Random 是一个工具类,用于生成各种随机数据。
Mock.Random 的方法在数据模板中称为『占位符』,书写格式为 @占位符(参数 [, 参数]) 。

var Random = Mock.Random
Random.email()
// => "[email protected]"
Mock.mock('@email')
// => "[email protected]"
Mock.mock( { email: '@email' } )
// => { email: "[email protected]" }

//引入mockjs
import Mock from 'mockjs'
const Random = Mock.Random;
//使用mockjs模拟数据
Mock.mock('/api/msdk/proxy/query_common_credit', (req, res) => {
  return {
    "ret":0,
    "data":
      {
        "mtime": Random.datetime(),//随机生成日期时间
        "score":  Random.natural(1, 800),//随机生成1-800的数字
        "rank":  Random.natural(1, 100),//随机生成1-100的数字
        "stars": Random.natural(0, 5),//随机生成1-5的数字
        "nickname": Random.cname()//随机生成中文名字
      }
  }
});

方法

Mock.Random 提供的完整方法(占位符)如下:

Type Method
Basic boolean, natural, integer, float, character, string, range, date, time, datetime, now
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

你可能感兴趣的:(Mock.Random)