public下新建version.json文件定义版本
{ "version":"1.1.0" }
util下新建updateVersion.js
import axios from 'axios'; import { Loading } from 'element-ui'; var t1; var t2; export async function isNewVersion() { var randomNumber=Math.random() const url = `//${window.location.host}/version.json?id=${randomNumber}`; const res = await axios.get(url); const version = res.data.version; const localVersion = localStorage.getItem('version'); if (localVersion && localVersion != version) { localStorage.setItem('version', version); const hide=()=>{ //我这里使用的是 let loadingInstance =Loading.service({ fullscreen: true,text:'版本正在更新渲染加载中...'}); clearTimeout(t1) } t1=setTimeout(hide,2000) t2=setTimeout(()=>{ clearTimeout(t2) window.location.reload(); },3000) } else { localStorage.setItem('version', version); } }
router的afterEach的后置守卫中加版本查询,更新代码
rotuer/index.js import {isNewVersion} from '@/util/updateVersion.js' router.afterEach((to,from)=>{ isNewVersion() }) export default router