微信小程序API交互反饋,wx.showToast顯示消息提示框

導讀:wx.showToast(OBJECT) 顯示消息提示框。 OBJECT參數說明: 參數 類型 必填 說明 最低版本 title String 是 提示的內容 icon String 否 圖標,有效值"success"、"loading" image String 否 自定義圖標的本地路...javascript

微信小程序API交互反饋,wx.showToast顯示消息提示框

wx.showToast(OBJECT)


顯示消息提示框。java

OBJECT參數說明:小程序

參數 類型 必填 說明 最低版本
title String 提示的內容  
icon String 圖標,有效值"success"、"loading"  
image String 自定義圖標的本地路徑,image 的優先級高於 icon 1.1.0
duration Number 提示的延遲時間,單位毫秒,默認:1500  
mask Boolean 是否顯示透明蒙層,防止觸摸穿透,默認:false  
success Function 接口調用成功的回調函數  
fail Function 接口調用失敗的回調函數  
complete Function 接口調用結束的回調函數(調用成功、失敗都會執行)  

示例代碼:微信小程序

wx.showToast({
  title: '成功', icon: 'success', duration: 2000 })

wx.showLoading(OBJECT)


基礎庫版本 1.1.0 開始支持,低版本需作兼容處理數組

顯示 loading 提示框, 需主動調用 wx.hideLoading 才能關閉提示框微信

OBJECT參數說明:ide

參數 類型 必填 說明
title String 提示的內容
mask Boolean 是否顯示透明蒙層,防止觸摸穿透,默認:false
success Function 接口調用成功的回調函數
fail Function 接口調用失敗的回調函數
complete Function 接口調用結束的回調函數(調用成功、失敗都會執行)

wx.hideToast()


隱藏消息提示框函數

wx.hideLoading()


基礎庫版本 1.1.0 開始支持,低版本需作兼容處理ui

隱藏loading提示框spa

wx.showLoading({
  title: '加載中', }) setTimeout(function(){ wx.hideLoading() },2000)

wx.showModal(OBJECT)


​顯示模態彈窗

OBJECT參數說明:

參數 類型 必填 說明
title String 提示的標題
content String 提示的內容
showCancel Boolean 是否顯示取消按鈕,默認爲 true
cancelText String 取消按鈕的文字,默認爲"取消",最多 4 個字符
cancelColor HexColor 取消按鈕的文字顏色,默認爲"#000000"
confirmText String 肯定按鈕的文字,默認爲"肯定",最多 4 個字符
confirmColor HexColor 肯定按鈕的文字顏色,默認爲"#3CC51F"
success Function 接口調用成功的回調函數
fail Function 接口調用失敗的回調函數
complete Function 接口調用結束的回調函數(調用成功、失敗都會執行)

succes返回參數說明:

參數 類型 說明 最低版本
confirm Boolean 爲 true 時,表示用戶點擊了肯定按鈕  
cancel Boolean 爲 true 時,表示用戶點擊了取消(用於 Android 系統區分點擊蒙層關閉仍是點擊取消按鈕關閉) 1.1.0

示例代碼:

wx.showModal({
  title: '提示', content: '這是一個模態彈窗', success: function(res) { if (res.confirm) { console.log('用戶點擊肯定') } else if (res.cancel) { console.log('用戶點擊取消') } } })

wx.showActionSheet(OBJECT)


​顯示操做菜單

OBJECT參數說明:

參數 類型 必填 說明
itemList String Array 按鈕的文字數組,數組長度最大爲6個
itemColor HexColor 按鈕的文字顏色,默認爲"#000000"
success Function 接口調用成功的回調函數,詳見返回參數說明
fail Function 接口調用失敗的回調函數
complete Function 接口調用結束的回調函數(調用成功、失敗都會執行)

success返回參數說明:

參數 類型 說明
tapIndex Number 用戶點擊的按鈕,從上到下的順序,從0開始

示例代碼:

wx.showActionSheet({
  itemList: ['A', 'B', 'C'], success: function(res) { console.log(res.tapIndex) }, fail: function(res) { console.log(res.errMsg) } })

Bug & Tip

  1. bug:Android6.3.30,wx.showModal 的返回的 confirm 一直爲 true;
  2. tip: wx.showActionSheet 點擊取消或蒙層時,回調fail, errMsg 爲 "showActionSheet:fail cancel";
  3. tip: wx.showLoading 和 wx.showToast 同時只能顯示一個,使用 wx.hideToast/wx.hideLoading 均可以關閉提示框;
  4. tipiOS wx.showModal 點擊蒙層不會關閉模態彈窗,因此儘可能避免使用「取消」分支中實現業務邏輯。
相關文章
相關標籤/搜索