微信小程序ES6封装全局请求类调用后端接口

news/2024/7/10 22:51:12 标签: 微信小程序, es6, 小程序

首先我们在项目根目录下建立config.js
config.js参考代码

const config = {
    api_base_url:'http地址',//接口前拼接的域地址
    appkey:"用户key"//当前用户的key值
}
export {config}

利用export导出config
之后我们在项目根节点建立文件夹util 在util下建立http.js
在http.js中导入config.js中的config
需要注意的是 用ES6导入的文件是不能用绝对路径的

import {config} from "../config.js"

之后我们在http.js中封装一个发请求的类

class HTTP{
    request(params){
        wx.request({
            url: config.api_base_url+params.url,
            method:params.method,
            data:params.data,
            header: {
                "content-type":"applion/json",
                "appkey":config.appkey
            },
            success:(res)=>{
                let Code = res.statusCode.toString()
                if(Code.startsWith("2")){
                    params.success(res.data);
                }else{
                    wx.showToast({
                        title:"服务器错误请稍后重试",
                        icon:"none",
                        duration:2000
                    })
                }
            },
            fail:(err)=>{
                wx.showToast({
                    title:"系统异常",
                    icon:"none",
                    duration:2000
                })
            }
        })
    }
}

最后导出这个类

export {HTTP}

之后我们就可以在具体组件的js中调用了
首先我们引入HTTP类并实例化

import {HTTP} from "../../util/http.js"

let http = new HTTP();

之后就可以通过实例化的类调用方法了

   http.request({
      url:'接口地址',
       method:'请求类型',
       data:{},
       success:(res)=>{
           console.log(res);
       }
     })

http://www.niftyadmin.cn/n/1038230.html

相关文章

Educational Codeforces Round 7总结

A题: A题题目链接 题目描述: A. Infinite Sequencetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputConsider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, …

规范:函数名本身就是最好的注释

其实规范这个东西可以很简单也可以很复杂,也许对你没什么帮助,有或许对你意义重大,因为我们编写任何一个项目都离不开规范 首先我要讲的是 花括号后面空格的问题 例如我们常见的 let closure function(data){if(data&&data.index){…

HDU5610——暴力枚举(可减少遍历次数)

题目描述: Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 827 Accepted Submission(s): 318 Problem DescriptionBaby Ming is fond of weight lifting. He has a …

HDU5620——斐波那契数列的应用

题目描述:KKs Steel Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 389 Accepted Submission(s): 184 Problem DescriptionOur lovely KK has a difficult mathematical problem:he has a N(1≤N…

HDU5621——数学应用(多边形内对角线交点个数) + 数论 + unsigned long long的应用

题目描述: KKs Point Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 713 Accepted Submission(s): 238 Problem DescriptionOur lovely KK has a difficult mathematical problem:He points N(2≤…

微信小程序wx:if和hidden有何不同 vue v-if和v-show有何不同

最近学习小程序越来越觉得他和前端目前的主流框架的语法真的是异曲同工。 微信小程序开发中 API给我们提供了两种条件渲染语句 wx:if和hidden <view wx:if {{name}}>条件渲染</view> <view hidden {{name}}>条件渲染</view>其实wx:if和hidden的区别…

HDU5605——数学题(三角函数的应用)

题目描述&#xff1a; geometry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 356 Accepted Submission(s): 269 Problem DescriptionThere is a point Pat coordinate (x,y).A line goes through the poin…

8VC Venture Cup 2016 - Elimination Round总结

A题&#xff1a; A题题目链接 题目描述&#xff1a; A. Robot Sequencetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputCalvin the robot lies in an infinite rectangular grid. Calvins source code contains a…