Vue 解决报错 VM6290:1 Uncaught SyntaxError: Unexpected identifier ‘Promise‘

Vue 报错 VM6290:1 Uncaught SyntaxError: Unexpected identifier 'Promise'

排查

控制台报了一个错误 , Uncaught SyntaxError: Unexpected identifier 'Promise',网上查到的方法是 缺少符号,语法写法错误,但这些都没有解决我的问题,于是开始了 一行一行代码注释,排查,找原因

一直以为 是使用了Promise出现的错误,将Promise相关代码注释掉,控制台还是会报这个错误

最终发现 是定时器 setTimeout()里 调用了该方法 导致控制台报的错

修改前代码


javascript 复制代码
  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() 中调用 方法改一下

修改后

javascript 复制代码
  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); 适用于一般函数

javascript 复制代码
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=>{
           //业务逻辑
     });
}
相关推荐
JohnYan6 分钟前
Bun技术评估 - 03 HTTP Server
javascript·后端·bun
开开心心就好37 分钟前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
難釋懷38 分钟前
Vue解决开发环境 Ajax 跨域问题
前端·vue.js·ajax
特立独行的猫a43 分钟前
Nuxt.js 中的路由配置详解
开发语言·前端·javascript·路由·nuxt·nuxtjs
中微子44 分钟前
小白也能懂:JavaScript 原型链和隐藏类的奇妙世界
javascript
咸虾米1 小时前
在uniCloud云对象中定义dbJQL的便捷方法
前端·javascript
梨子同志1 小时前
JavaScript Proxy 和 Reflect
前端·javascript
海的诗篇_1 小时前
移除元素-JavaScript【算法学习day.04】
javascript·学习·算法
汤圆炒橘子1 小时前
状态策略模式的优势分析
前端
__Yx__1 小时前
JavaScript核心概念输出——原型链
javascript