程序笔记   发布时间:2022-07-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了mockjs数据占位符大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

数据占位符定义规范DPD

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

内置方法列表:

@H_645_9@method @H_363_17@miscellaneous
Type
Basic Boolean, natural, Integer, float, character, String, range, date, time, datetiR_384_11845@e, 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
guid, id

1. 基本方法

可以生成随机的基本数据类型:


  let mock = require('mockjs')
  let data = mock.mock({
      'list|10': [{
          'id|+1': 1,
          'name':'@String',//字符串
          'point':'@Integer',//整形
          'birthday':'@date'//日期
      }]
  })

// 输出结果
console.log(JSON.Stringify(data,null,2))

2. 图像方法

  • image 随机生成图片地址

let mock = require('mockjs')
let data = mock.mock({
    'list|10': [{
        'id|+1': 1,
        'name':'@String',
        'point':'@Integer',
        'birthday':'@date',
        'pic':'@image'//随机生成图片地址
    }]
})
// 输出结果
console.log(JSON.Stringify(data,null,2))

3. 文本方法

  • @title: 标题。
  • @cword(100):文本内容,参数为字数。

let mock = require('mockjs')
let data = mock.mock({
    'list|10': [{
        'id|+1': 1,
        'name':'@String',
        'point':'@Integer',
        'birthday':'@date',
        'pic':'@image',
        'title':'@title',//标题
        'content':'@cword(100)'//文本内容
    }]
})
// 输出结果
console.log(JSON.Stringify(data,null,2))

4. 名称方法

  • cname :中文名称
  • cfirst:中文姓氏
  • Last:英文姓氏
let mock = require('mockjs')
let data = mock.mock({
    'list|10': [{
        // 属性 id 是一个自增数,起始值为 1,每次增 1
        'id|+1': 1,
        'name':'@cname',
        'ename':'@last',
        'cfirst':'@cfirst',
        'point':'@Integer',
        'birthday':'@date',
        'pic':'@image',
        'title':'@title',
        'content':'@cword(100)'
    }]
})
// 输出结果
console.log(JSON.Stringify(data,null,2))

5. 网络方法

  • 可以生成url/ip/email等网络相关信息
let mock = require('mockjs')
let data = mock.mock({
    'list|10': [{
        'id|+1': 1,
        'name':'@cname',
        'ename':'@last',
        'cfirst':'@cfirst',
        'point':'@Integer',
        'birthday':'@date',
        'pic':'@image',
        'title':'@title',
        'content':'@cword(100)',
        'url':"@url",//url地址
        'ip':"@ip",//ip地址
        'email':"@email"//邮政
    }]
})
// 输出结果
console.log(JSON.Stringify(data,null,2)

6. 地址方法

  • @region 区域
  • @county 省市县
let mock = require('mockjs')
let data = mock.mock({
    'list|10': [{
        'id|+1': 1,
        'name':'@cname',
        'ename':'@last',
        'cfirst':'@cfirst',
        'point':'@Integer',
        'birthday':'@date',
        'pic':'@image',
        'title':'@title',
        'content':'@cword(100)',
        'url':"@url",
        'ip':"@ip",
        'email':"@email",
        'area':'@region',//区域
        'address':'@county(true)'//省市区
    }]
})
// 输出结果
console.log(JSON.Stringify(data,null,2))

大佬总结

以上是大佬教程为你收集整理的mockjs数据占位符全部内容,希望文章能够帮你解决mockjs数据占位符所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。