微信小程序开发日记(二)

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

所以今天来看另外三个(后来发现其实就是一个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和另外两个接口的布尔值来决定后续要不要使用上一篇介绍的那三个获取用户信息的接口。

然后看对应的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)的部分
其中参数部分的写法为

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

参数说明

  • ${API} 代表 API 名字
  • ${method} 代表调用方式,有效值为return, success, object, callback
  • ${param} 代表参数或者返回值
  • ${option} 代表参数的可选值或者返回值的属性
  • ${component} 代表组件名字
  • ${attribute} 代表组件属性
  • ${option} 代表组件属性的可选值

这样就对上了,一种学习思路

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容