猿问

Javascript 表达式不理解

我是 React 和 Js 的新手,我想了解这行代码(它是 JSX 内部的 Js):


<h5 className="recipes__title">

  {item.recipe.label < 20 ? `${item.recipe.label}` : `${item.recipe.label.substring(0, 25)}...` }

</h5>

任何人都知道如何阅读和理解它?


呼唤远方
浏览 77回答 2
2回答

一只名叫tom的猫

<h5 className="recipes__title">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //An html header&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Containing...&nbsp;&nbsp; {&nbsp; &nbsp; item.recipe.label < 20 ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// If the item.recipe.label is less than 20 then...`${item.recipe.label}`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// the label: `${item.recipe.label.substring(0, 25)}&nbsp; &nbsp;//else the first 25 characters of the label followed by&nbsp;&nbsp; ...`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// the string "..."&nbsp; &nbsp; &nbsp;}</h5>您可以在此处找到有关三元运算符(有条件地解析为两个表达式之一的表达式)的信息您可以在此处找到有关模板文字(可以包含要解析的 javascript 的字符串)的信息

慕仙森

JSX 部分:<element> {&nbsp;//&nbsp;You&nbsp;can&nbsp;put&nbsp;your&nbsp;Javascript&nbsp;here&nbsp;but&nbsp;mostly&nbsp;inline&nbsp;script.&nbsp;} </element`${...}`这是 ES6 中引入的字符串模板。它用于构建字符串。${}表示要处理JS,变量名或函数调用。子串(0, 25)这是检查标签是否最多 25 个字符的部分。如果不是,它会选择前 25 个字符,然后在其后添加省略号(...)。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答