首页 > 解决方案 > 如何将 html 标签作为 JavaScript 参数的一部分传递

问题描述

第一次必须这样做,所以请多多包涵。

我创建了一个帮助函数,它允许 React 应用程序使用英语和法语版本的文本。例子:

{outputEnFr(
  "Currently Availability:",
  "Disponibilité actuelle:",
  this.props.lang
)}

上面的英文文本在辅助函数(“当前可用性:”)中,我需要传入下面的 html:

<p>
   <strong>Yes</strong> = available and ready in our yard
</p>
<p>
   <strong>No</strong> = currently on rent, on contract
   or being serviced. Please contact us to reserve this
   unit.
</p>

如何将这段 html 代码块传递到上面辅助函数英文区域的 JavaScript 参数中?

我尝试过但不起作用的方法:

{outputEnFr(
"
  <p>
     <strong>Yes</strong> = available and ready in our yard
  </p>
  <p>
     <strong>No</strong> = currently on rent, on contract
     or being serviced. Please contact us to reserve this
     unit.
  </p>
",
"Disponibilité actuelle:",
this.props.lang
)}

标签: javascriptreactjs

解决方案


你的策略很奇怪。如果您需要翻译您的应用程序,只需使用 react-18next 或任何类似的库(Talkr 等),而不是将完整的 html 块作为参数传递。所以它会这样:

 <p><strong>{t("yes")}</strong> {t("yard.availability")}</p>
 <p> <strong>{t("no")}</strong>{t("rent.contact")}</p>


推荐阅读