gpt4 book ai didi

javascript - 如何在 Next JS Application 中将 String 转换为 HTML 元素?

转载 作者:行者123 更新时间:2023-12-05 00:30:32 24 4
gpt4 key购买 nike

我已将产品描述作为 HTML 元素保存到这样的数据库中。但是当我将这些数据渲染到 div 中时,它显示为字符串。我想在我的 中显示所有数据,例如 HTML 元素下一个JS申请 .我尝试了 JSON.parse,但它不起作用。
如果有人可以帮助我解决这个问题,我们将不胜感激。

{
"images": [
{
"alternativeText": "cinnamon",
"path": "public/uploads/5585.jpg"
}
],
"_id": "606c39c884372a0e20c3f9bb",
"name": "Cinnamon",
"code": "5586",
"price": 49,
"category": {
"_id": "6064b37855bd0f26e0df4616",
"name": "Natural Essential Oils",
"createdAt": "2021-03-31T17:38:00.066Z",
"updatedAt": "2021-03-31T17:38:24.398Z",
"__v": 0
},
"description": "<p>Cinnamon Bark Vitality&trade; essential oil has a warm, spicy flavor that complements a variety of classic culinary treats and drinks. Cinnamon Bark Vitality is not only great for elevating dishes, but it can also be taken as a dietary supplement and is an important ingredient in some of Young Living&rsquo;s most popular products, including Thieves&reg; Vitality&trade; and Inner Defense&trade;. Cinnamon Bark Vitality&rsquo;s warm taste and nostalgic notes bring a spicy addition to your favorite dishes. By using Cinnamon Bark Vitality as a dietary supplement, you can help support healthy digestive and immune systems.</p>",
"createdAt": "2021-04-06T10:36:56.440Z",
"updatedAt": "2021-04-06T10:36:56.440Z",
"__v": 0
}

检查图像附件。
enter image description here
下一个 Js 应用代码
const Product = ({product}) => {
return (
<Fragment>
<Head>
<title>Product</title>
</Head>
<Layout>
<div className="container-fluid product-page">
<div className="page-body">
<div className="row product">
<div className="col-xl-5 col-lg-5 image-box">
<div className="preview-image">
<div className="image">
<img
src={ImageRootPath + "/" + product.images[0].path}
alt={product.images[0].alternativeText}
/>
</div>
</div>
</div>
<div className="col-xl-7 col-lg-7 info-box">
<div className="product-info">
<div className="product-name">
<h4>{product.name}</h4>
<p>{"Product code: " + product.code}</p>
<hr/>
</div>
<div className="product-price">
<h5>Price: <span>{product.price + "$"}</span></h5>
<p>{"Quantity: 5ml"}</p>
</div>
<div className="product-des">
{product.description}
</div>
</div>
</div>
</div>
</div>
</div>
</Layout>
</Fragment>
);
}

最佳答案

因为你想显示原始 HTML,所以这个

<div className="product-des">
{product.description}
</div>
应该改为
<div className="product-des" dangerouslySetInnerHTML={{ __html: product.description }}>
official doc 对此进行了介绍

dangerouslySetInnerHTML is React’s replacement for using innerHTML in the browser DOM [...] you have to type out dangerouslySetInnerHTML and pass an object with a __html key


而且您还必须注意在这种情况下设置原始 HTML 的风险

In general, setting HTML from code is risky because it’s easy to inadvertently expose your users to a cross-site scripting (XSS) attack

关于javascript - 如何在 Next JS Application 中将 String 转换为 HTML 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67034874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com