Mock

The installation

  • npm i mockjs

use

  • Create a file, for example named index.js, and import Mockjs
  • Import index.js in main

Fixed the API

  • Disclaimer: everything with c is in Chinese, and everything without C is in English
  • 'the string | 4' : 'oh'Generate a string, | four is four, | 1-4 1-4 are random, don’t write a
  • string: '@cword()'@cword() generates a text in which (2) is two and (1,9) is one to nine
  • title:'@ctitle(1)',@ctitle() randomly generates a title,(5) is a title of five strings,(1-5) is random between one and five, starting with c is Chinese
  • sentence:'@csentence(1)',@csentence() generates sentences randomly,(1) is only 1 string,(1-5) is 1 to 5 random
  • content:'@cparagraph(2)'@cParagraph () randomly generates a paragraph,(1) is only one sentence,(1-9) one to nine sentences
  • 'number|1-100':10Generates a number 10 and number | 1 to generate the number 1, number | 1-100 to generate a random number 1-100
  • id:'@increment(1)',@increment() generates a different ID by +1 from the previous one
  • name:'@cname()Random name generation
  • idC:'@id()Random id generation
  • address:'@city()',Random generated address,() in the province with true, do not add is not with
  • img:'@image()',@image() randomly generates an image,(' 250 x250 ', '# FFF', '# 000', 'the PNG', 'kun kun),Generate a 250×250 PNG image with a background color of # FFF and a font color of #000
  • date:'@date()‘Generation time, only year month day,(yyyy-MM-dd hh:mm:ss) with year, month, day, hour, minute, second})
  • Request methods look at code blocks
// Define the GET request
// Mock.mock('/api/news', 'get', {
// status: 200,
// MSG: 'get successful ',
// data
// })
The return argument can be an object or a function, in which case a return is required
//Mock addresses support regex. Get requests will fail if they are not matched with regex
Mock.mock(/\/api\/news/.'get'.(options) = > {
    return {
        status: 200.msg: 'get succeeded ',
        data
    }
})
// Define a POST request
Mock.mock('/api/news'.'post', {
    status: 200.msg: 'Post obtained successfully',
    data
})
Copy the code
  • Specific method of use
const data = Mock.mock({
    'list|8': [/ / the list is to generate a 'list | 8' generated eight arrays, 'list | 8 to 20' generate 8 to 20 array, the list is the custom
        {
            name: '@cname()'.address: '@city(true)'.id: '@increment(1)'}]})Copy the code