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=>{
           //业务逻辑
     });
}
相关推荐
来一碗刘肉面11 分钟前
React - ajax 配置代理
前端·react.js·ajax
bin915322 分钟前
DeepSeek 助力 Vue 开发:打造丝滑的二维码生成(QR Code)
前端·javascript·vue.js·ecmascript·deepseek
@LitterFisher26 分钟前
Excell 代码处理
前端·javascript·excel
十八朵郁金香2 小时前
【JavaScript】深入理解模块化
开发语言·javascript·ecmascript
m0_748230942 小时前
Redis 通用命令
前端·redis·bootstrap
YaHuiLiang2 小时前
一切的根本都是前端“娱乐圈化”
前端·javascript·代码规范
菜鸟一枚在这3 小时前
深入解析设计模式之单例模式
开发语言·javascript·单例模式
ObjectX前端实验室3 小时前
个人网站开发记录-引流公众号 & 谷歌分析 & 谷歌广告 & GTM
前端·程序员·开源
CL_IN3 小时前
企业数据集成:实现高效调拨出库自动化
java·前端·自动化
浪九天5 小时前
Vue 不同大版本与 Node.js 版本匹配的详细参数
前端·vue.js·node.js