首页 >web前端 >uni-app > 正文

uniapp如何封装request请求

原创2021-01-12 17:27:3012533

uniapp封装request请求的方法:首先项目下新建common文件夹,再创建【request.js】文件;然后打开【request.js】文件,开始写封装的代码;最后通过promise异步请求,最后导出方法。

本教程操作环境:windows7系统、uni-app2.5.1版本、thinkpad t480电脑。

推荐(免费):uni-app开发教程

uniapp封装request请求的方法:

1、项目下新建common文件夹,再创建request.js文件

586ee9a55ade2f47cad57f7129e6973.png

2、打开request.js文件,开始写封装的代码

思路很简单

  • 定义域名:baseUrl;

  • 定义方法:api;

通过promise异步请求,最后导出方法。

request.js参考代码如下

const baseUrl = 'https://unidemo.dcloud.net.cn'   
const request = (url = '', date = {}, type = 'GET', header = {
}) => {
    return new Promise((resolve, reject) => {
        uni.request({
            method: type,
            url: baseUrl + url,
            data: date,
            header: header,
            dataType: 'json',         
        }).then((response) => {
            setTimeout(function() {
                uni.hideLoading();
            }, 200);
            let [error, res] = response;        
            resolve(res.data);
        }).catch(error => {
            let [err, res] = error;
            reject(err)
        })
    });
}
export default request

3、在main.js全局注册

import request from 'common/request.js'
Vue.prototype.$request = request

a123546200accbd76c65b54636da05c.png

4、页面调用

this.$request('/api/news', {
// 传参参数名:参数值,如果没有,就不需要传
}).then(res => {
// 打印调用成功回调
console.log(res)
})

页面调用的index.vue

<template>
    <view>
        <uni-list v-for="(item,index) in productList" :key="index">
            <uni-list-item :title="item.author_name" :note="item.title"></uni-list-item>
        </uni-list>
    </view>
</template>
<script>
    import uniList from "@/components/uni-list/uni-list.vue"
    import uniListItem from "@/components/uni-list-item/uni-list-item.vue"
    export default {
        components: {
            uniList,
            uniListItem
        },
        data() {
            return {
                productList: [],
            };
        },
        onLoad() {
            this.getList();
        },
        methods: {
            getList() {
                this.$request('/api/news', {
                    // 传参参数名:参数值,如果没有,就不需要传
                    // "username": "john",
                    // "key": this.searchValue
                }).then(res => {
                    // 打印调用成功回调
                    console.log(res)
                    this.productList = res;
                })
            },
        }
    }
</script>
<style>
</style>

相关免费学习推荐:编程视频

以上就是uniapp如何封装request请求的详细内容,更多请关注php中文网其它相关文章!

php中文网最新课程二维码

声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理

  • 相关标签:uniapp request请求
  • 相关文章

    相关视频


      网友评论

      文明上网理性发言,请遵守 新闻评论服务协议

      我要评论条评论
      达拉崩巴斑得贝迪浪
    • 达拉崩巴斑得贝迪浪· 2021-03-10 19:16:571楼
    • 如果第一个参数是对象的话,用以下写法: if (typeof(url) != 'string') { let rs = url; url = rs.url; rs.data=rs.data?rs.data:{} data = Object.assign(rs.data,data); typex = rs['method']; header = rs.header

    • 专题推荐

      作者信息

      coldplay.xixi

      好好学习 天天向上

      最近文章
      重点详解Java类和对象1393
      浅析php简单操作mysql锁机制2997
      php无法加载mysql怎么办1577
      推荐视频教程
    • 你的第一行 UNI-APP 代码你的第一行 UNI-APP 代码
    • 视频教程分类