How to create and use the mockData (simulated data) of the WeChat applet?

The MockData (simulated data) of the WeChat applet allows developers to quickly develop and debug front-end without relying on back-end services. The steps to create and use MockData are as follows:

  1. Create a mock folder under the root directory of the applet, and create a corresponding data file in this folder, for example: mock/demo.json.

  2. Edit the data file, the format must meet the JSON format specification, and contain the interface data information that needs to be simulated.

  3. When requesting the API interface in the applet code, modify the request address to the corresponding MockData address, for example:

//原始接口请求
wx.request({
    
    
  url: 'https://api.example.com/list',
  data: {
    
     ... },
  success: res => {
    
     ... }
})

//使用MockData接口请求
wx.request({
    
    
  url: '/mock/demo.json', //修改为MockData地址
  data: {
    
     ... },
  success: res => {
    
     ... }
})

  1. In the applet development tool, enable the "Enhanced Compilation" function to automatically monitor the changes of the MockData data file when running the project, and update the simulation data in real time to improve development efficiency.

It should be noted that before using MockData, it is necessary to confirm that it can truly simulate the data format and status code returned by the backend, and switch to the real interface backend service in time after the development is completed to ensure user experience and data accuracy.

Guess you like

Origin blog.csdn.net/qq_37609787/article/details/131051656