Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the advantages of js mock

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the advantages of js mock". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the advantages of js mock"?

The problem solved by mock is that at the time of development, the back end has not completed the data output, so the front end has to write static simulation data. The data is too long, write the data in the js file, and change the url one by one after completion. For some code with complex logic, you have to be careful when adding or removing simulated data. To restore as much real data as possible, either write more code or manually modify the simulated data. Special formats, such as IP, random numbers, pictures, addresses, need to be collected.

Advantages of mock

1. Separation of front and rear ends

Let the front-end engineer develop independently of the back-end.

2. Increase the authenticity of the test

Various scenarios are simulated by random data.

3. Non-intrusive development

You can intercept Ajax requests and return simulated response data without modifying the existing code.

4. Easy to use

An intuitive interface.

5. Rich data types

Support the generation of random text, numbers, Boolean values, dates, mailboxes, links, pictures, colors, etc.

6. Easy to expand

Support for extension of more data types, support for custom functions and regularities.

7. In the case of existing interface documents, we can directly follow the interface documents to develop and write the corresponding fields. After the completion of the interface, we only need to change the url address.

8. No cross-domain issues are involved

Mock case demo

1. First, you need to download and install the mockjs module

Cnpminstallmockjs-S

two。 You can then create a folder under scr for files such as / src/mock/index.js that hold the simulated data, in this file

Copy the code

ConstMock=require ("mockjs")

Letdata=Mock.mock ({

"data | 100": [/ / generate an array of 100 data items

{

"shopId | + 1": 1 shopId control / generated product id, self-increment 1

"shopMsg": "@ ctitle (10)", / / generate commodity information with a length of 10 Chinese characters

"shopName": "@ cname", / / generate commodity names, all Chinese names

"shopTel": / ^ 1 (5 | 3 | 7 | 8) [0-9] {9} $/, / generate a random phone number

"shopAddress": "@ county (true)", / / randomly generated address

At this point, I believe you have a deeper understanding of "what are the advantages of js mock?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report