微信小程序開發(fā)日記(二)

昨天看了三個獲取用戶信息的接口,但是挺奇怪的,貌似是要跟其他接口配合使用

所以今天來看另外三個(后來發(fā)現(xiàn)其實就是一個wx.canIUse,這里注意一下,只不過為了可讀性,我們可以寫一寫明確的訪問請求)微信接口

  • wx.canIUse
  • canIUseGetUserProfile
  • canIUseOpenData

為什么今天看這三個呢?

<block wx:if="{{canIUseOpenData}}">
      <view class="userinfo-avatar" bindtap="bindViewTap">
        <open-data type="userAvatarUrl"></open-data>
      </view>
      <open-data type="userNickName"></open-data>
    </block>
    <block wx:elif="{{!hasUserInfo}}">
      <button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 獲取頭像昵稱 </button>
      <button wx:elif="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 獲取頭像昵稱 </button>
      <view wx:else> 請使用1.4.4及以上版本基礎庫 </view>
    </block>
    <block wx:else>
      <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
      <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    </block>

這里有個,if,elseif,else結構,通過判斷canIUseGetUerProfile和另外兩個接口的布爾值來決定后續(xù)要不要使用上一篇介紹的那三個獲取用戶信息的接口。

然后看對應的Typescript部分。

canIUse: wx.canIUse('button.open-type.getUserInfo'),
canIUseGetUserProfile: false,
    // canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需嘗試獲取用戶信息可改為false

然后再看文檔中關于
boolean wx.canIUse(string schema)的部分
其中參數(shù)部分的寫法為

${API}.${method}.${param}.${option} 或者 ${component}.${attribute}.${option}

參數(shù)說明

  • ${API} 代表 API 名字
  • ${method} 代表調用方式,有效值為return, success, object, callback
  • ${param} 代表參數(shù)或者返回值
  • ${option} 代表參數(shù)的可選值或者返回值的屬性
  • ${component} 代表組件名字
  • ${attribute} 代表組件屬性
  • ${option} 代表組件屬性的可選值

這樣就對上了,一種學習思路

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容