Share the usage of a vueui axios-mock-adapter

Source: Internet
Author: User

Import Axios from ' Axios '; import mockadapter from ' Axios-mock-adapter '; import {loginusers, Users} from '. /mockdata/user '; let _users = users;export default {/** * mock bootstrap */bootstrap () {Let mock = new Mockadap    ter (Axios);    Mock success Request Mock.onget ('/success '). Reply ($, {msg: ' success '});    Mock error request Mock.onget ('/error '). Reply ($, {msg: ' failure '});      Login Mock.onpost ('/login '). Reply (config = {let {username, password} = json.parse (Config.data);        return new Promise ((resolve, reject) = {let user = null; SetTimeout (() = {Let Hasuser = loginusers.some (U = = {if (U.username = = = Username && u              . Password = = = password) {user = Json.parse (json.stringify (U));              User.password = undefined;            return true;          }          });         if (hasuser) {resolve ([$, {code:200, msg: ' Request succeeded ', user}]); } else {Resolve ([$, {code:500, msg: ' Account or password error '}]);      }}, 500);    });    });      Gets the user list mock.onget ('/user/list '). Reply (config + = {let {name} = Config.params;        Let mockusers = _users.filter (user = = (name && user.name.indexOf (name) = =-1) return false;      return true;      }); return new Promise ((resolve, reject) = {SetTimeout (() = {Resolve ([Users:mocku        SERS}]);      }, 500);    });    });      Get the user list (paging) mock.onget ('/user/listpage '). Reply (config + = {Let {page, name} = Config.params;        Let mockusers = _users.filter (user = = (name && user.name.indexOf (name) = =-1) return false;      return true;      });      Let total = mockusers.length;      Mockusers = Mockusers.filter (u, index) = Index < * page && index >= (page-1)); return new Promise ((resolve, reject) = {SettimEout (() = {Resolve ([, {total:total, users:mockusers}]);      }, 500);    });    });      Delete User mock.onget ('/user/remove '). Reply (config + = {let {id} = config.params;      _users = _users.filter (U = u.id!== id);            return new Promise ((resolve, reject) = {SetTimeout (() = {Resolve ([code:200        Msg: ' Delete succeeded '}]);      }, 500);    });    });      Edit User Mock.onget ('/user/edit '). Reply (config = {let {id, name, addr, age, birth, sex} = Config.params;          _users.some (U = = {if (U.id = = = id) {u.name = name;          u.addr = addr;          U.age = age;          U.birth = birth;          U.sex = sex;        return true;      }      });            return new Promise ((resolve, reject) = {SetTimeout (() = {Resolve ([code:200        Msg: ' Edit successful '}]);      }, 500);  });  });      Added user mock.onget ('/user/add '). reply (config = = {Name, addr, age, birth, sex} = Config.params;      _users.push ({name:name, addr:addr, Age:age, Birth:birth, sex:sex});            return new Promise ((resolve, reject) = {SetTimeout (() = {Resolve ([code:200        Msg: ' New Success '}]);      }, 500);    });  }); }};

  

Share the usage of a vueui axios-mock-adapter

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.