swan.canIUse
解释: 判断智能小程序的API,回调,参数,组件等是否在当前版本可用。
方法参数
String schema
schema 的表达形式
${API}.${method}.${param}.${option}${class}.${method}.${param}.${option}${component}.${attribute}.${option}返回值
Boolean 当前版本是否可用Error对象。schema参数说明
示例
扫码体验
请使用百度APP扫码图片示例
代码示例 1
在开发者工具中预览效果 -
-<view class="wrap"> <button type="primary" bindtap="canIUse">canIUse</button></view>
-Page({ canIUse() { // 组件 console.log('view.hover-class', swan.canIUse('view.hover-class')); console.log('scroll-view.scroll-x', swan.canIUse('scroll-view.scroll-x')); console.log('cover-view', swan.canIUse('cover-view')); console.log('button.size.default', swan.canIUse('button.size.default')); // API: ${method} 为 object console.log('request.object.method.OPTIONS', swan.canIUse('request.object.method.OPTIONS')); // API: ${method} 为 success console.log('ai.imageAudit.success.data.stars.name', swan.canIUse('ai.imageAudit.success.data.stars.name')); // API: ${method} 为 callback console.log('onAppShow.callback.entryType.user', swan.canIUse('onAppShow.callback.entryType.user')); // API: ${method} 为 return console.log('getEnvInfoSync.return.env.trial', swan.canIUse('getEnvInfoSync.return.env.trial')); // API: 类 console.log('VideoContext.requestFullScreen.object.direction', swan.canIUse('VideoContext.requestFullScreen.object.direction')); console.log('CanvasContext.fill', swan.canIUse('CanvasContext.fill')); }});
.wrap { padding: 50rpx 30rpx;}
代码示例2 - 判断智能小程序的某个API是否在当前版本可用 :
在开发者工具中预览效果 -
-<button data-name="getSystemInfo" type="primary" bindtap="bindCanIUse">swan.getSystemInfo</button>
Page({ bindCanIUse(e) { let canIuseResult = swan.canIUse(e.currentTarget.dataset.name); // true swan.showToast({ title: JSON.stringify(canIuseResult), icon: 'none' }); }});
代码示例3 - 判断智能小程序的某个API的调用方式是否在当前版本可用 :
在开发者工具中预览效果 -
-<button data-name="getSystemInfo.success" type="primary" bindtap="bindCanIUse">getSystemInfo.success</button>
Page({ bindCanIUse(e) { let canIuseResult = swan.canIUse(e.currentTarget.dataset.name); // true swan.showToast({ title: JSON.stringify(canIuseResult), icon: 'none' }); }});
代码示例4 - 判断智能小程序的某个API的调用方式的返回值是否在当前版本可用 :
在开发者工具中预览效果 -
-<button data-name="getSystemInfoSync.return.screenWidth" type="primary" bindtap="bindCanIUse">getSystemInfoSync.return.screenWidth</button>
Page({ bindCanIUse(e) { let canIuseResult = swan.canIUse(e.currentTarget.dataset.name); // true swan.showToast({ title: JSON.stringify(canIuseResult), icon: 'none' }); }});
代码示例5 - 判断智能小程序的某个API的调用方式返回的参数可选值是否在当前版本可用 :
在开发者工具中预览效果 -
-<button data-name="chooseImage.success.tempFiles.path" type="primary" bindtap="bindCanIUse">chooseImage.success.tempFiles.path</button>
Page({ bindCanIUse(e) { let canIuseResult = swan.canIUse(e.currentTarget.dataset.name); // false swan.showToast({ title: JSON.stringify(canIuseResult), icon: 'none' }); }});
代码示例6 - 判断智能小程序的某个组件是否在当前版本可用 :
在开发者工具中预览效果 -
-<button data-name="text" type="primary" bindtap="bindCanIUse">text</button>
Page({ bindCanIUse(e) { let canIuseResult = swan.canIUse(e.currentTarget.dataset.name); // true swan.showToast({ title: JSON.stringify(canIuseResult), icon: 'none' }); }});
代码示例7 - 判断智能小程序的某个组件属性是否在当前版本可用 :
在开发者工具中预览效果 -
-<button data-name="text.selectable" type="primary" bindtap="bindCanIUse">text.selectable</button>
Page({ bindCanIUse(e) { let canIuseResult = swan.canIUse(e.currentTarget.dataset.name); // true swan.showToast({ title: JSON.stringify(canIuseResult), icon: 'none' }); }});
代码示例8 - 判断智能小程序的某个组件属性的可选值是否在当前版本可用 :
在开发者工具中预览效果 -
-<button data-name="button.open-type.contact" type="primary" bindtap="bindCanIUse">button.open-type.contact</button>
Page({ bindCanIUse(e) { let canIuseResult = swan.canIUse(e.currentTarget.dataset.name); // true swan.showToast({ title: JSON.stringify(canIuseResult), icon: 'none' }); }});
Bug&Tip
- -
swan.canIUse('request.success.data');
.或空格的属性不做支持;- 类型,校验方式举例如下:
// swan.ai.textReview Array.<Object>swan.canIUse('ai.textReview.success.result.reject.label');// swan.chooseImage Array.<string>swan.canIUse('chooseVideo.object.sourceType.album');
