MOCKJS Basic Introduction to the separation of front and rear end

Source: Internet
Author: User

Installation and use
# 安装npm install mockjs
#使用 Mockvar Mock = require(‘mockjs‘)var data = Mock.mock({    // 属性 list 的值是一个数组,其中含有 1 到 10 个元素    ‘list|1-10‘: [{        // 属性 id 是一个自增数,起始值为 1,每次增 1        ‘id|+1‘: 1    }]})// 输出结果console.log(JSON.stringify(data, null, 4))
Mock.mock (): Generate analog data from data template 1.rurl
可选。表示需要拦截的 URL,可以是 URL 字符串或 URL 正则。例如 /\/domain\/list\.json/、‘/domian/list.json‘。
2.rtype
可选。表示需要拦截的 Ajax 请求类型。例如 GET、POST、PUT、DELETE 等。
3.template
可选。表示数据模板,可以是对象或字符串。例如 { ‘data|1-10‘:[{}] }、‘@EMAIL‘。
4.function (Options)
可选。表示用于生成响应数据的函数。options指向本次请求的 Ajax 选项集,含有 url、type 和 body 三个属性
Mock.setup ()
配置拦截 Ajax 请求时的行为。支持的配置项有:timeout。
Mock.setup({    timeout: 400})Mock.setup({    timeout: ‘200-600‘})指定被拦截的 Ajax请求的响应时间,单位是毫秒。值可以是正整数,例如400,表示 400 毫秒 后才会返回响应内容;也可以是横杠 ‘-‘风格的字符串,例如 ‘200-600‘,表示响应时间介于 200 和600 毫秒之间。默认值是‘10-100‘。
Mock.random
var Random = Mock.RandomRandom.email()// => "[email protected]"Mock.mock(‘@email‘)// => "[email protected]"Mock.mock( { email: ‘@email‘ } )// => { email: "[email protected]" }

MOCKJS Basic Introduction to the separation of front and rear end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.