6_Introduce mock

Official documentation: http://mockjs.com/

1. Installation
  • install dependenciesnpm i mockjs -D
2. Configuration
  • Create a directory mock, create a new mock configuration file (divided by module)
import Mock from "mockjs";
// 延时后再响应
Mock.setup({
    
    
  timeout: 300,
});
Mock.mock("/rules-setting/get", "get", () => {
    
    
  if (verifyToken()) {
    
    
    // 继续业务操作,返回成功或失败
    return {
    
    
      result: true,
      code: 200,
      data: rulesSetting,
      msg: "success!",
    };
  }
  // token校验失败,返回611错误码;前端收到,跳转登录接口。
  return resData(false, 611, "expired token!");
});
3. Introduce it in the entry file index.js to enable it. Comments are discontinued.
import "./mock/rules-setting";
4. Q&A

【Notice】

  1. [Pit] When axios.defaults.baseURL = '/api' is set, the mock will fail and the request will not be intercepted. So when using mock, don't set baseURL;
  2. The request path must be consistent with the interception path, otherwise the request cannot be intercepted.

Guess you like

Origin blog.csdn.net/weixin_42465316/article/details/129292125