在根目錄app.jsx文件中的componentDidMount方法中添加代碼
//判斷目前微信版本是否支持自動更新
if(Taro.canIUse("getUpdateManager")){
const update=Taro.getUpdateManager();
update.onCheckForUpdate((res)=>{
//檢測是否有新版本
if(res.hasUpdate){
update.onUpdateReady(()=>{
// update.applyUpdate();
//如果有新版本,給用戶提示確認更新即可
Taro.showModal({
title: '更新提示',
content: '新版本已經準備好,是否重啓應用?',
success: function (res) {
if (res.confirm) {
// 新的版本已經下載好,調用 applyUpdate 應用新版本並重啓,如果想靜默更新,直接在檢測有新版本的時候調用此方法即可
update.applyUpdate();
}
}
})
})
//如果自動更新失敗,給用戶提示手動更新(有些小程序涉及到多ID使用,不更新會出現莫名的緩存bug,大部分小程序不用執行這一步)
update.onUpdateFailed(()=>{
Taro.showModal({
title: '已經有新版本了',
content: '新版本已經上線,請您刪除當前小程序,重新打開。'
})
})
}
})
}else{
Taro.showModal({
title: '提示',
content: '當前微信版本過低,無法使用該功能,請升級到最新微信版本後重試。'
})
}