后浪云百度小程序教程:swan.canIUse

  • swan.canIUse
    • 方法参数
      • schema 的表达形式
      • 返回值
      • schema 参数说明
    • 示例
      • 图片示例
      • 代码示例 1
      • 代码示例 2:判断智能小程序的某个 API 是否在当前版本可用
      • 代码示例 3:判断智能小程序的某个 API 的调用方式是否在当前版本可用
      • 代码示例 4:判断智能小程序的某个 API 的调用方式的返回值是否在当前版本可用
      • 代码示例 5:判断智能小程序的某个 API 的调用方式返回的参数可选值是否在当前版本可用
      • 代码示例 6:判断智能小程序的某个组件是否在当前版本可用
      • 代码示例 7:判断智能小程序的某个组件属性是否在当前版本可用
      • 代码示例 8:判断智能小程序的某个组件属性的可选值是否在当前版本可用
    • Bug & Tip

    swan.canIUse

    解释: 判断智能小程序的 API ,回调,参数,组件等是否在当前版本和当前系统下可用。

    方法参数

    String schema

    schema 的表达形式

    • ${API}.${method}.${param}.${option}
    • ${class}.${API}.${method}.${param}.${option}
    • ${component}.${attribute}.${option}

    返回值

    Boolean 当前版本是否可用
    入参类型错误时,会抛出一个标准的Error对象。

    schema 参数说明

    参数 说明

    ${API}

    API 名字

    ${class}

    类名

    ${method}

    调用方式,有效值为:return、object、回调函数的名称(多数为 success 和 callback)

    ${param}

    参数或者返回值

    ${option}

    参数的有效值或者返回值的属性或者组件属性的有效值

    ${component}

    组件名字

    ${attribute}

    组件属性

    示例

    扫码体验

    代码示例

    后浪云百度小程序教程:swan.canIUse插图

    请使用百度APP扫码

    图片示例

    后浪云百度小程序教程:swan.canIUse插图1

    代码示例 1

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
     
     
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <view class="top-description border-bottom">view.hover-class</view>
    4. <button data-name="view.hover-class" type="primary" bindtap="canIUse">canIUse</button>
    5. </view>
    6. <view class="card-area">
    7. <view class="top-description border-bottom">scroll-view.scroll-x</view>
    8. <button data-name="scroll-view.scroll-x" type="primary" bindtap="canIUse">canIUse</button>
    9. </view>
    10. <view class="card-area">
    11. <view class="top-description border-bottom">cover-view</view>
    12. <button data-name="cover-view" type="primary" bindtap="canIUse">canIUse</button>
    13. </view>
    14. <view class="card-area">
    15. <view class="top-description border-bottom">button.size.default</view>
    16. <button data-name="button.size.default" type="primary" bindtap="canIUse">canIUse</button>
    17. </view>
    18. <view class="card-area">
    19. <view class="top-description border-bottom">request.object.method.OPTIONS</view>
    20. <button data-name="request.object.method.OPTIONS" type="primary" bindtap="canIUse">canIUse</button>
    21. </view>
    22. <view class="card-area">
    23. <view class="top-description border-bottom">ai.imageAudit.success.data.stars.name</view>
    24. <button data-name="ai.imageAudit.success.data.stars.name" type="primary" bindtap="canIUse">canIUse</button>
    25. </view>
    26. <view class="card-area">
    27. <view class="top-description border-bottom">onAppShow.callback.entryType.user</view>
    28. <button data-name="onAppShow.callback.entryType.user" type="primary" bindtap="canIUse">canIUse</button>
    29. </view>
    30. <view class="card-area">
    31. <view class="top-description border-bottom">getEnvInfoSync.return.env.trial</view>
    32. <button data-name="getEnvInfoSync.return.env.trial" type="primary" bindtap="canIUse">canIUse</button>
    33. </view>
    34. <view class="card-area">
    35. <view class="top-description border-bottom">VideoContext.requestFullScreen.object.direction</view>
    36. <button data-name="VideoContext.requestFullScreen.object.direction" type="primary" bindtap="canIUse">canIUse</button>
    37. </view>
    38. <view class="card-area">
    39. <view class="top-description border-bottom">CanvasContext.fill</view>
    40. <button data-name="CanvasContext.fill" type="primary" bindtap="canIUse">canIUse</button>
    41. </view>
    42. </view>

    代码示例 2:判断智能小程序的某个 API 是否在当前版本可用

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
     
     
     
    1. <button data-name="getSystemInfo" type="primary" bindtap="bindCanIUse">swan.getSystemInfo</button>

    代码示例 3:判断智能小程序的某个 API 的调用方式是否在当前版本可用

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
     
     
     
    1. <button type="primary" bindtap="bindCanIUse">getSystemInfo.success</button>

    代码示例 4:判断智能小程序的某个 API 的调用方式的返回值是否在当前版本可用

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
     
     
     
    1. <button type="primary" bindtap="bindCanIUse">getSystemInfoSync.return.screenWidth</button>

    代码示例 5:判断智能小程序的某个 API 的调用方式返回的参数可选值是否在当前版本可用

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
     
     
     
    1. <button type="primary" bindtap="bindCanIUse">chooseImage.success.tempFiles.path</button>

    代码示例 6:判断智能小程序的某个组件是否在当前版本可用

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
     
     
     
    1. <button type="primary" bindtap="bindCanIUse">text</button>

    代码示例 7:判断智能小程序的某个组件属性是否在当前版本可用

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
     
     
     
    1. <button type="primary" bindtap="bindCanIUse">text.selectable</button>

    代码示例 8:判断智能小程序的某个组件属性的可选值是否在当前版本可用

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
     
     
     
    1. <button type="primary" bindtap="bindCanIUse">button.open-type.contact</button>

    Bug & Tip

    • 回调函数的名称以文档为准。
    • 不支持 fail 和 complete 回调函数的判断。
    • 支持 success 回调参数的判断,举例如下:
     
     
     
    1. swan.canIUse('request.success.data');
    • 纯 number 类型的属性不做支持。
    • 带有.或空格的属性不做支持。
    • 如果参数是 Array.<object> 或 Array.<string> 类型,校验方式举例如下:
     
     
     
    1. // swan.ai.textReview Array.<object>
    2. swan.canIUse('ai.textReview.success.result.reject.label');
    3. // swan.chooseImage Array.<string>
    4. swan.canIUse('chooseVideo.object.sourceType.album');
    THE END