Vue 報錯 VM6290:1 Uncaught SyntaxError: Unexpected identifier ‘Promise’
排查
控制臺報了一個錯誤 , Uncaught SyntaxError: Unexpected identifier ‘Promise’,網上查到的方法是 缺少符號,語法寫法錯誤,但這些都沒有解決我的問題,于是開始了 一行一行代碼注釋,排查,找原因
一直以為 是使用了Promise
出現的錯誤,將Promise相關代碼注釋掉
,控制臺還是會報這個錯誤
最終發現 是定時器 setTimeout()里
調用了該方法 導致控制臺報的錯
修改前代碼
mounted() {this.init();this.getBoxStatus();let timer = null;if (timer) {clearInterval(timer);} else {timer = setInterval(() => {setTimeout(this.getBoxStatus(), 0);}, 1000);}this.$once("hook:beforeDestroy", () => {clearInterval(timer);});},async getBoxStatus() {let data = {cid: this.$route.params.cid,};const res = await getDuoBoxStatusDto(data);if (res && res.length > 0) {if (res[0] && res[0].cid) {const [r1 = {}, r2 = {}] = await Promise.all([videodownloading({ cid: `${res[0].cid}_1` }),//調接口videodownloading({ cid: `${res[0].cid}_2` }),//調接口]);this.SMD_UrlA01 = r1.data? "data:image/png;base64," + res.data: this.defultSmdImg;this.SMD_UrlA02 = r2.data? "data:image/png;base64," + res.data: this.defultSmdImg;}},
解決辦法
只需要將 setTimeOut()
中調用 方法改一下
修改后
mounted() {this.init();this.getBoxStatus();let timer = null;if (timer) {clearInterval(timer);} else {timer = setInterval(() => {setTimeout(async () => {this.getBoxStatus();}, 0);}, 1000);}this.$once("hook:beforeDestroy", () => {clearInterval(timer);});},
附
setTimeout(this.getBoxStatus(), 0);
適用于一般函數
mounted() {this.init();this.getBoxStatus();let timer = null;if (timer) {clearInterval(timer);} else {timer = setInterval(() => {setTimeout(this.getBoxStatus(), 0);}, 1000);}this.$once("hook:beforeDestroy", () => {clearInterval(timer);});},
methods(){getBoxStatus(){let data = {cid: this.$route.params.cid,};getDuoBoxStatusDto(data).then(res=>{//業務邏輯});
}