为什么会出现意外的模板字符串表达式错误? [英] Why do I get an unexpected template string expression error?

查看:131
本文介绍了为什么会出现意外的模板字符串表达式错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 react-structured-data JSX内使用地图数组数据( $ {adv_event.title} )?

Is it possible to use map array data (${adv_event.title}) inside a react-structured-data JSX?

我尝试添加反引号,但未成功: name:`$ {adv_event.title}`",

I tried adding backticks with no success: name: "`${adv_event.title}`",

尝试1:

<Generic jsonldtype="event" schema={{
  name: "${adv_event.title}", 
  description: "",
  startDate: "YYYY-MM-DDT:HH:MM",
  endDate: "YYYY-MM-DDT:HH:MM",
  image: "",
}}>

错误:

296:31警告意外的模板字符串表达式字符串中没有模板

296:31 warning Unexpected template string expression no-template-curly-in-string

推荐答案

这是ESLint生成的警告:无模板的字符串中的卷曲

This is an warning generated by ESLint: no-template-curly-in-string

禁止使用常规字符串中的模板文字占位符语法(无字符串模板)

Disallow template literal placeholder syntax in regular strings (no-template-curly-in-string)

ECMAScript 6允许程序员创建包含变量的字符串或使用模板文字而不是字符串的表达式通过在两个之间编写类似 $ {variable} 的表达式进行连接反引号(`).在以下情况下使用错误的引号很容易想要使用模板文字,通过编写"$ {variable}" 并最终使用文字值"$ {variable}" 而不是包含注入的表达式的值.

ECMAScript 6 allows programmers to create strings containing variable or expressions using template literals, instead of string concatenation, by writing expressions like ${variable} between two backtick quotes (`). It can be easy to use the wrong quotes when wanting to use template literals, by writing "${variable}", and end up with the literal value "${variable}" instead of a string containing the value of the injected expressions.

如果您只想分配该变量,则应执行以下操作:

If you want to just assign that variable you should do this:

<Generic jsonldtype="event" schema={{
    name: adv_event.title, 
    description: "",
    startDate: "YYYY-MM-DDT:HH:MM",
    endDate: "YYYY-MM-DDT:HH:MM",
    image: "",
}}>

您的情况下不需要模板字符串.

A template string is not needed in your case.

这篇关于为什么会出现意外的模板字符串表达式错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆