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

2022-04-28  本文已影响0人  糖醋沼跃鱼

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

所以今天来看另外三个(后来发现其实就是一个wx.canIUse,这里注意一下,只不过为了可读性,我们可以写一写明确的访问请求)微信接口

为什么今天看这三个呢?

<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}

参数说明

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

上一篇 下一篇

猜你喜欢

热点阅读