Compatibility

The component and API of Mini Program are improving and enriching, so some old sdk may not support the newly added abilities, so developers should handle the compatibility issue.

The my.canIUse(String) can help to detect whether the API, input parameter or returned value, component, property is supported in current version.

Handle Newly Added API

Following codes show how to handle compatibility issue for newly added API.

copy
if (my.getLocation) {
	my.getLocation();
} else {
    my.alert({
       title: 'Hint',
       content: 'The function is not supported, please upgrade App'
    });
}

Handle Newly Added Input Parameter of API

copy
if (my.canIUse('getLocation.object.type')) {
    // ...
} else {
	console.log('The parameter is not supported')
}

Handle Newly Added Return Value of API

copy
if (my.canIUse('getSystemInfo.return.storage')) {
	// ...
} else {
	console.log('The returned value is not supported')
}

Handle Newly Added Property of Component

copy
Page({
  data: {
    canIUse: my.canIUse('button.open-type.share')
  }
})
copy
<button a:if="{{canIUse}}" open-type="share">Share</button>
<button a:else onTap="shareApp">Share Mini Program</button>