tt.canIUse
收藏
我的收藏

基础库 1.35.0 开始支持本方法,这是一个同步方法。​
判断小程序的 API、回调、参数、组件、Page 或 Component 实例的属性和方法等是否在当前版本可用。​

语法​

text
复制
tt.canIUse(schema)

参数说明​

schema​

类型​
默认值​
必填​
说明​
最低支持版本​
string​
是​
用来测试的能力,模式为 ${API}.${method}.${param}.${option} 或 ${component}.${attribute}.${option} 或 Page.${method} 或者 Page.${attribute} 或 Component.${method} 或 Component.${attribute}​
1.35.0​

schema 详细说明​

值​
说明​
最低支持版本​
${API}​
API 名字​
1.35.0​
${method}​
调用方式,有效值为 return、success、object、callback​
1.35.0​
${param}​
参数或者返回值​
1.35.0​
${option}​
参数的有效值或者返回值的属性或者组件属性的有效值​
1.35.0​
${component}​
组件名字​
1.35.0​
${attribute}​
组件属性​
1.35.0​

返回值​

返回值是一个布尔值,表示在当前版本是否可用。​

扫码体验​

代码示例​

js
复制
// 对象的属性或方法
tt.canIUse("UpdateManager");
tt.canIUse("Stats.isDirectory");
// 接口参数、回调或者返回值
tt.canIUse("exitMiniProgram");
tt.canIUse("getLaunchOptionsSync.return.path");
tt.canIUse("getSystemInfo.success.screenWidth");
tt.canIUse("showToast.object.title");
tt.canIUse("onCompassChange.callback.direction");
tt.canIUse("request.object.method.GET");
// 组件的属性
tt.canIUse("swiper");
tt.canIUse("text.selectable");
tt.canIUse("button.open-type.contact");
// Page 函数的 this 上的方法
tt.canIUse("Page.provide");
tt.canIUse("Page.setProvide");
// Component 函数的 this 上的方法
tt.canIUse("Component.inject");
tt.canIUse("Component.observers");

Bug & Tip​

    Tip:回调函数的名称以文档为准;​
    Tip:不支持 fail 和 complete 回调函数的判断;​
    Tip:支持 success 回调参数的判断,例如:tt.canIUse("request.success.data");​
    Tip:纯 number 类型的属性不做支持。​