如何在 Next.js 应用程序中将字符串转换为 HTML 元素?

9
我将产品描述以HTML元素的形式保存到数据库中,但是在将这些数据渲染到
时,它们以字符串的形式显示。我想在我的Next JS应用程序中像HTML元素一样显示所有数据。我尝试了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
}

请查看附加的图片。 在这里输入图片描述

下一个 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元素”是什么意思?您是指必须显示HTML标签吗? - ksa
1个回答

25

由于您想要显示原始的HTML,所以这样

<div className="product-des">
  {product.description}
</div>

应该被改为

<div className="product-des" dangerouslySetInnerHTML={{ __html: product.description }}>

这在官方文档中有介绍。

dangerouslySetInnerHTML 是 React 用来替代在浏览器 DOM 中使用 innerHTML 的方法[...] 使用时需要输入 dangerouslySetInnerHTML 并传递一个带有 __html 键的对象。

同时,你还应该注意设置原始 HTML 的风险,例如:

通常情况下,通过代码设置 HTML 是很危险的,因为很容易意外地让用户面临跨站脚本(XSS)攻击。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接