簡體   English   中英

vue:v-html 可以在 js 數組中嗎?

[英]vue: can v-html be inside a js array?

我有一個包含數組和對象的 js 文件。 我需要從這些 arrays 中設置一些屬性的樣式。 例如

myArray= [
  {
    id: 1,
    name: 'honda',
    description: 'to buy the latest car, click here'
  },
  {
    id: 2,
    name: 'tesla',
    description: 'to buy the latest car, click here'
  }
]

假設我想設置description屬性的樣式,以便我可以在那里綁定一個鏈接。 我認為這樣做的方法是使用原始 html 但就我而言,我將把它包含在我的數組中。 這可能嗎? 我試圖到處搜索這個問題,但沒有對此案的解釋。 太感謝了。

您可以在數組中使用任何 html 樣式的代碼,如下所示。

myArray= [
  {
    id: 1,
    name: 'honda',
    description: '<a href="https://www.honda.com">to buy the latest car, click here</a>'
  },
  {
    id: 2,
    name: 'tesla',
    description: '<a href="https://www.tesla.com" style="font-size:50px">to buy the latest car, click here</a>'
  }
]

在你的模板中你會使用:比如

...
<div v-for="car in myArray" :key="car.id">
    <p v-html="car.description"></p>
</div>

查看此代碼。

看看這個代碼筆。

您可以查看vuejs 文檔以獲取更多信息。

暫無
暫無

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

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