Compatibility

The capabilities of Mini Programs are continuously increasing, but older versions of WeChat clients do not support new capabilities, so it is required to achieve compatibility when using these new capabilities.

This document will provide the version number supported by each features when describing the components, APIs, etc.

You can get the base library version number of the Mini Program via wx.getSystemInfo or wx.getSystemInfoSync.

You can also determine whether the corresponding APIs or components can be directly used under such base library version according to the details of wx.canIUse.

How to Achieve Compatibility - Version Comparison

The version number format of the WeChat client and the Mini Program base library is Major.Minor.Patch (major version number.minor version number.revision number). The developers can achieve compatibility according to the version number. The following is the reference code:

function compareVersion(v1, v2) {
  v1 = v1.split('.')
  v2 = v2.split('.')
  var len = Math.max(v1.length, v2.length)

  while (v1.length < len) {
    v1.push('0')
  }
  while (v2.length < len) {
    v2.push('0')
  }

  for (var i = 0; i < len; i++) {
    var num1 = parseInt(v1[i])
    var num2 = parseInt(v2[i])

    if (num1 > num2) {
      return 1
    } else if (num1 < num2) {
      return -1
    }
  }

  return 0
}

compareVersion('1.11.0', '1.9.9')
// 1

How to Achieve Compatibility - Interface

For the new APIs, you can use the following code to determine whether the user's phone is supported.

if (wx.openBluetoothAdapter) {
  wx.openBluetoothAdapter()
} else {
  // If you want users to experience your Mini Program on the latest version of the client, you can provide the following prompt:
  wx.showModal({
    title: 'Note',
    content: 'The current WeChat version is too low to use this feature. Please upgrade to the latest WeChat version and try again.'
  })
}

How to Achieve Compatibility - Parameter

You can use the following code to determine whether the API or return value contains new parameters.

wx.showModal({
  success: function(res) {
    if (wx.canIUse('showModal.cancel')) {
      console.log(res.cancel)
    }
  }
})

How to Achieve Compatibility - Component

The new components or attributes will not be processed on older versions, but they will not cause an error either. If a special scene requires downgrading the old version, you can perform the following operation.

Page({
  data: {
    canIUse: wx.canIUse('cover-view')
  }
})
<video controls="{{!canIUse}}">
  <cover-view wx:if="{{canIUse}}">play</cover-view>
</video>

个结果 ""

    没有找到相关内容 ""