美文网首页
微信小程序开发日记(二)

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

作者: 糖醋沼跃鱼 | 来源:发表于2022-04-28 23:27 被阅读0次

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

所以今天来看另外三个(后来发现其实就是一个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} 代表组件属性的可选值

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

相关文章

网友评论

      本文标题:微信小程序开发日记(二)

      本文链接:https://www.haomeiwen.com/subject/dheeyrtx.html