篇首语:本文由小编为大家整理,主要介绍了基于Promise封装uni-app的request方法,实现类似axios形式的请求相关的知识,希望对你有一定的参考价值。
https://my.oschina.net/u/2428630/blog/3004860
uni-app
框架中
npm install uni-request --save
import uniRequest from "uni-request";
uniRequest.request(config)uniRequest.get(url[, config])uniRequest.delete(url[, config])uniRequest.head(url[, config])uniRequest.options(url[, config])uniRequest.post(url[, data[, config]])uniRequest.put(url[, data[, config]])uniRequest.patch(url[, data[, config]])
uniRequest.defaults.baseURL = "https://yourapi.domain.com";uniRequest.defaults.headers.common["Authorization"] = AUTH_TOKEN;uniRequest.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
// 向具有给定ID的用户发出请求uniRequest.get("/user?id=12345") .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });// 可选地,上面的请求也可以按照uniRequest.get("/user", { data: { id: "number" }}).then(function (response) { console.log(response);}).catch(function (error) { console.log(error);});// 想要使用 async/await? 将`async`关键字添加到外部函数/methodasync function getUser() { try { const response = await uniRequest.get("/user?ID=12345"); console.log(response); } catch (error) { console.error(error); }}
uniRequest.post("/user", { firstname : "firstname", lastname : "lastname" }).then(function (response) { console.log(response);}).catch(function (error) { console.log(error);});
以上就是基本用法,如果掌握了就可以使用了uni-request
以上是关于基于Promise封装uni-app的request方法,实现类似axios形式的请求的主要内容,如果未能解决你的问题,请参考以下文章