小程序API —— 51小程序界面交互 - loading 提示框

小程序提供了一些用于界面交互的 API,例如 loading 提示框、消息提示框、模态对话框等 API;

loading 提示框常配合网络请求来使用,用于提高用户体验,对应的 API 有两个:

  • wx.showLoading() 显示 loading 提示框;
  • wx.hideLoading() 关闭 loading 提示框;

注意 loading 提示框显示之后不会自动关闭,必须主动调用 hideLoading 方法才能关闭 loading 提示框;

接下来我们使用这两个 API 来实现一个需求:

当用户点击按钮时,小程序会发送请求获取数据,在发送请求过程中需要显示 loading 提示框,同时给用户文字提示;当数据请求完成之后,关闭 loading 提示框;

下面打开微信开发者工具来实现这个需求:

在 pages/cate/cate.js 中添加下面代码:

javascript 复制代码
Page({

  data: {
    list: []
  },
  getData(){

    // 显示 loading 提示框
    wx.showLoading({
      // title 用来显示提示的内容
      // 提示的内容不会自动换行,如果提示的内容比较多,超出行的内容会被隐藏
      title: '数据正在加载中...',
      // 是否展示透明蒙层,防止触摸穿透,true 表示隐藏
      mask: true
    })

    // 发起网络请求,需要使用 wx.request API
    wx.request({
      // 接口地址
      url: 'https://gamll-prod.atguigu.cn/mall-api/index/findBanner',
      // 请求方式
      method: 'GET',
      // 请求参数
      data: {},
      // 请求头
      header: {},
      // API 调用成功以后,执行的回调
      success: (res) => {
        if(res.data.code === 200){
          this.setData({
            list: res.data.data
          })
        }
      },
      // API 调用失败以后,执行的回调
      fail: (err) => {
        console.log(err);
      },
      // API 不管调用成功还是失败,都会执行的回调
      complete: (res) => {
        // console.log(res)

        // 关掉 loading 提示框
        wx.hideLoading()
      }
    })
  }
})

在 pages/cate/cate.wxml 中添加下面代码:

html 复制代码
<button type="warn" bind:tap="getData">获取数据</button>

编译运行, 点击按钮的时候,可以发现出现了 loading 提示框,如下:

参考视频:尚硅谷微信小程序开发教程

相关推荐
2501_915918412 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张2 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
破无差3 天前
《赛事报名系统小程序》
小程序·html·uniapp
00后程序员张3 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬3 天前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app
2501_915106323 天前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
亮子AI3 天前
【小程序】微信小程序隐私协议
微信小程序·小程序
weixin_177297220693 天前
短剧小程序系统开发:打造个性化娱乐新平台
小程序·娱乐·短剧
weixin_lynhgworld3 天前
剧本杀小程序系统开发:开启沉浸式社交娱乐新纪元
小程序·娱乐
~废弃回忆 �༄3 天前
mobx-miniprogram小程序的数据传输
小程序