我在Vue JS中做了一個小博客,呈現出帶有標題和描述的文章。我希望能在描述中使用markdown,所以我使用了npm install marked。
當我在資料中硬編碼一個文本時,它作業得很好,但試圖傳遞我的描述,這本身來自于一個道具,它不作業了。
請看我下面的代碼,我注釋了不作業的地方,并提供了一個硬編碼資料的例子。
有什么辦法可以解決這個問題嗎?謝謝!
<template>
<div class="til-list">
<div v-for="til in tils" 。 key="til">
<div class="til"/span>>
<h3>{{ til.title }}</h3>
<p>{ til.description }}</p>
<div v-html="markdownToHtml"/span>> </div>>
<!-- <div v-html="markdownToHtml(til.description)"></div> -->
<span v-for="target in til. tags" :key="tag"> #{{ tag }}. </span> #{{ tag }}.
</div>/span>
</div>/span>
</div>/span>
</template>
<script>。
import marked from "marked"。
export default {
props: ["tils"]。
// computed: {
// markdownToHtml(description) {
// return marked(this.description);
// },
// },
data(){
return {
markdown: "# Hello World"/span>,
};
},
computed: {
markdownToHtml() {
return marked(this.markdown) 。
},
},
};
</script>
uj5u.com熱心網友回復:
嘗試用methods代替computed屬性:
new Vue({
el: '#demo',
//props: ["tils"],
data(){
return {
tils: [{title: 'title 1', description: '<b>text</b> '}, {title: 'title 2', description: '<i>text</i>'}] 。
};
},
methods: {
markdownToHtml(description) {
return marked(description)。
},
}
})
<script src="https://cdnjs. cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>/span>
<script src="https://cdn. jsdelivr.net/npm/marked/marked.min.js"></script>
<div id="demo">/span>
<div class="til-list">/span>
<div v-for=" (til, index) in tils" 。 key="index"/span>>
<div class="til">/span>
<h3>{{ til.title }}</h3>
<p>{ til.description }}</p>
<div v-html="markdownToHtml(til. description)"></div>。
<!--<span v-for="tag in til.tags" :key="tag"> #{{ tag }} </span>-->
</div>
</div>/span>
</div>/span>
</div>/span>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/310019.html
標籤:
上一篇:帶圓點的按鈕式分頁
下一篇:不能將型別'System.Collections.Generic.IEnumerable<string>'隱式轉換為'string'。
