微信小程序(四十)API的封装与调用

注释很详细,直接上代码

上一篇

新增内容:
1.在单独的js文件中写js接口
2.以注册为全局wx的方式调用接口

源码:

utils/testAPI.js

js 复制代码
const testAPI={
    /**
     * 
     * @param {*} title 
     */
    simpleToast(title='提示'){//可传参,默认为'提示'
        wx.showToast({
          title:title,
          icon:'none',
          duration:2000,
          mask:true//加上蒙版防止误触
        })
    }

}

//导出
export default testAPI

app.js

js 复制代码
import testAPI from "./utils/testAPI"
//注册到全局wx对象上,当然也可以在页面的js里面注册单独给某个页面使用
wx.testAPI=testAPI

App({
    globalData: {

    },

})

index.js

js 复制代码
Page({
    
   onLoad(){  //小程序开始时调用
    wx.testAPI.simpleToast('hello world')
   }
});

效果演示:

相关推荐
00后程序员张10 小时前
python 抓包在实际项目中的合理位置,结合代理抓包、设备侧抓包与数据流分析
android·ios·小程序·https·uni-app·iphone·webview
2501_9159184117 小时前
使用 HBuilder 上架 iOS 应用时常见的问题与应对方式
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074719 小时前
iOS 崩溃日志的分析方法,将崩溃日志与运行过程结合分析
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074719 小时前
React Native 混淆在真项目中的方式,当 JS 和原生同时暴露
javascript·react native·react.js·ios·小程序·uni-app·iphone
00后程序员张20 小时前
苹果应用商店上架App流程,签名证书、IPA 校验、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074720 小时前
iOS 上架需要哪些准备,围绕证书、描述文件和上传方式等关键环节展开分析
android·ios·小程序·https·uni-app·iphone·webview
qq_124987075320 小时前
基于微信小程序的私房菜定制上门服务系统(源码+论文+部署+安装)
java·spring boot·微信小程序·小程序·毕业设计·毕设
2501_9151063220 小时前
iOS 上架费用解析,哪些成本可以通过流程优化降低。
android·ios·小程序·https·uni-app·iphone·webview
换日线°21 小时前
微信小程序找不同游戏(有效果图)
游戏·微信小程序
风月歌21 小时前
小程序项目之超市售货管理平台小程序源代码(源码+文档)
java·微信小程序·小程序·毕业设计·源码