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=>{
           //业务逻辑
     });
}
相关推荐
狂炫一碗大米饭2 分钟前
快刷每日面经😋
前端·面试
烛阴6 分钟前
Express + Prisma + MySQL:一站式搭建高效 Node.js 后端服务
javascript·后端·express
涵信9 分钟前
第二节:React 基础篇-受控组件 vs 非受控组件
前端·javascript·react.js
二月垂耳兔79825 分钟前
jQueryHTML与插件
前端·jquery
quo-te1 小时前
AJAX简介
前端·ajax·okhttp
bingbingyihao1 小时前
通过代码获取接口文档工具
开发语言·前端·javascript
月伤591 小时前
JS中Map对象与数组的相互转换
前端·javascript·html
SEO_juper2 小时前
解密 URL 参数:如何利用它们提升网站性能和用户体验
前端·javascript·ux·seo·url·数字营销·谷歌seo
nuIl2 小时前
让 Cursor 帮你把想法落地
前端·ai编程
HyaCinth3 小时前
Taro 数字滚动组件
javascript·react.js·taro