【问题标题】:Parsing error: Unexpected token, expected "," JavaScript React解析错误:意外的令牌,预期的“,” JavaScript React
【发布时间】:2026-01-10 06:40:02
【问题描述】:

我试图在组件内部传递纯 JavaScript 代码以在 index.js 渲染中使用。我要使用的代码是一个文本动画,它还有一个 HTML 文件和 CSS。

附:我很新来反应

    update() {
    let output = "";
    let complete = 0;
    for (let i = 0, n = this.queue.length; i < n; i++) {
      let { from, to, start, end, char } = this.queue[i];
      if (this.frame >= end) {
        complete++;
        output += to;
      } else if (this.frame >= start) {
        if (!char || Math.random() < 0.28) {
          char = this.randomChar();
          this.queue[i].char = char;
        }
        output += `<span class="dud">${char}</span>`;
      } else {
        output += from;
      }
    }
    this.el.innerHTML = output;
    if (complete === this.queue.length) {
      this.resolve();
    } else {
      this.frameRequest = requestAnimationFrame(this.update);
      this.frame++;
    }
  }
  randomChar() {
    return this.chars[Math.floor(Math.random() * this.chars.length)];
  }
}
const phrases = ["Welcome,", "To My Website"];
const el = document.querySelector(".text");
const fx = new TextScramble(el);
let counter = 0;
const next = () => {
  fx.setText(phrases[counter]).then(() => {
    setTimeout(next, 800);
  });
  counter = (counter + 1) % phrases.length;
};

我不断收到以下错误。

** Line 61:1:  Parsing error: Unexpected token, expected ","

  59 |   }
  60 | }
> 61 | const phrases = ["Welcome,", "To My Website"];
     | ^
  62 | 
  63 | const el = document.querySelector(".text");
  64 | const fx = new TextScramble(el);**

【问题讨论】:

    标签: javascript reactjs parsing react-redux components


    【解决方案1】:

    您没有向我们展示整个代码。有了这里的内容,您在第 60 行有一个无与伦比的 }。

    你应该有:

    function update () {
    ...
    function randomChar () {
    ...
    

    【讨论】:

      最近更新 更多