当前位置: 代码网 > 移动>腾讯>微信 > 微信小程序开发系列(二十九)·界面交互API·loading 提示框、showModal模态对话框、showToast消息提示框

微信小程序开发系列(二十九)·界面交互API·loading 提示框、showModal模态对话框、showToast消息提示框

2024年08月02日 微信 我要评论
小程序提供了一些用于界面交互的 API,例如:loading 提示框、消息提示框、模态对话框等 API。

目录

1.  loading 提示框

1. 1  wx.showloading()显示loading提示框

1.2  wx.hideloading()关闭 loading 提示框

2.  showmodal 模态对话框

3.  showtoast 消息提示框


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

1.  loading 提示框

1. 1  wx.showloading()显示loading提示框

        找到 index.js文件,添加代码:

    // 显示 loading 提示框
    wx.showloading({
      title: '数据正在加载中...',
    })

        完整的index.js代码:


page({

  data:{
    list:[]
  },
  // 获取数据
  getdata(){

    // 显示 loading 提示框
    wx.showloading({
      title: '数据正在加载中...',
    })

    // 如果需要发起网络请求,需要使用 wx.request api
    wx.request({
      // 接口地址
      url: 'https://gmall-prod.atguigu.cn/mall-api/index/findbanner',
      // 请求方式
      method:"get",
      // 请求参数,若没有则为空,什么也不写
      data:{},
      // 请求头,这里不需要暂时不写
      header:{},
      // api 调用成功以后,执行的回调
      success:(res)=>{
        // console.log(res)
        if(res.data.code == 200){
          this.setdata({
            list: res.data.data
          })
        }
      },
      // api 调用失败以后,执行的回调
      fail:(err)=>{
        console.log(err)
      },
      //  api 无论成功或者失败,执行的回调
      complete:(res)=>{
        console.log(res)
      }
    })

  }
})

        根据上图我们会发现,并没有将“数据正在加载...”中的三个点加载出来,那是因为提示的内容不会自动换行,如果提示的内容比较多,因为在同一行展示,多出来的内容就会被隐藏掉,我们可以将数据放的少一点,来进行解决:

        不过此时,我们再次点击“获取数据”会发现,仍能进行点击:

        那是因为此时的mask默认为:false

        此时的代码可以理解为:

    // 显示 loading 提示框
    wx.showloading({
      title: '数据加载中...',
      // 是否展示透明蒙层,防止触摸穿透
      mask: false
    })

        将false改为true,则点击一次后,会出现透明蒙层,无法在进行点击:

    // 显示 loading 提示框
    wx.showloading({
      title: '数据加载中...',
      // 是否展示透明蒙层,防止触摸穿透
      mask: true
    })

1.2  wx.hideloading()关闭 loading 提示框

        打开折叠的 wx.request 函数,找到complete更改其内容:

      complete:(res)=>{
        // console.log(res)
        // 关闭 loading 提示框
        wx.hideloading()
      }

        则会发现,点击完后,展示内容,提示框关闭:

2.  showmodal 模态对话框

        找到index.wxml文件,创建一个按钮:

<button type="primary" bind:tap="delhandler">删除商品</button>

        找到index.js文件,给按钮添加相关属性:

  // 删除商品
  async delhandler(){
    
    // showmodal 显示模态对话框
    const res = await wx.showmodal({
      title: '提示',
      content: '是否删除该商品?'
    })

    console.log(res)
  }

         点击“删除商品”:

        点击“取消”:

        点击“确定”:

3.  showtoast 消息提示框

        编写代码:

  // 删除商品
  async delhandler(){
    
    // showmodal 显示模态对话框
    const { confirm } = await wx.showmodal({
      title: '提示',
      content: '是否删除该商品?'
    })
    // console.log(res)
    if(confirm){
      // showtoast消息提示框
      wx.showtoast({
        title: '删除成功',
        // 不显示图标
        icon:"none",
        // 消息提示框两秒后自动关闭
        duration:2000
      })
    }else{
      wx.showtoast({
        title: '取消删除',
        icon:"error",
        // 消息提示框两秒后自动关闭
        duration:2000
      })
    }
  }

        点击取消:

        点击确定:

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com