问题描述
是否可以在 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: "",
}}>
错误:
推荐答案
这是ESLint生成的警告:无模板的字符串中的卷曲
This is an warning generated by ESLint: 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.
这篇关于为什么会出现意外的模板字符串表达式错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!