【问题标题】:React.js: Unexpected template string expression no-template-curly-in-stringReact.js:意外的模板字符串表达式 no-template-curly-in-string
【发布时间】:2022-01-01 15:39:59
【问题描述】:
handleNext = async () => {
    console.log("Next");
    let url =
      'https://newsapi.org/v2/top-headlines?country=in&apiKey=eae0696c32814d94af8f1f9c908cb49d?&page=${this.state.page + 1}';
    let data = await fetch(url);
    let parsedData = await data.json();

    this.setState({
      page: this.state.page + 1,
      articles: parsedData.articles,
    });
  };

当我尝试使用 ${this.state.page + 1} 时出现错误:Unexpected template string expression no-template-curly-in-string and the url did not get the value of this prop

【问题讨论】:

  • 看看rule details,您应该会找到答案。 (您应该在字符串周围使用反引号(`),而不是引号('))

标签: node.js reactjs


【解决方案1】:

正如@George 所说,您必须使用反引号 ` 而不是引号 ' 来插入字符串。

handleNext = async () => {
    console.log("Next");
    let url =
      `https://newsapi.org/v2/top-headlines?country=in&apiKey=eae0696c32814d94af8f1f9c908cb49d?&page=${this.state.page + 1}`;
    let data = await fetch(url);
    let parsedData = await data.json();

    this.setState({
      page: this.state.page + 1,
      articles: parsedData.articles,
    });
  };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 2018-08-16
    相关资源
    最近更新 更多