簡體   English   中英

如何在 Next JS Applcation 中將字符串轉換為 HTML 元素?

[英]How to convert String into a HTML element in Next JS Applcation?

我已將產品描述作為 HTML 元素保存到這樣的數據庫中。 但是當我將這些數據渲染到 div 中時,它顯示為字符串。 我想在我的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,所以這個

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

應該改為

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

這在官方文檔中有介紹

dangerouslySetInnerHTML是 React 在瀏覽器 DOM 中使用innerHTML的替代品 [...] 你必須輸入dangerouslySetInnerHTML使用__html傳遞 object

在這種情況下,您還必須注意設置原始 HTML 的風險

通常,從代碼中設置 HTML 是有風險的,因為很容易無意中將您的用戶暴露給跨站點腳本 (XSS) 攻擊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM