您的位置:首页 > 教育 > 培训 > 潍坊住房公积金官网_潍坊网站建设方案推广_搜一搜搜索_营销网站定制

潍坊住房公积金官网_潍坊网站建设方案推广_搜一搜搜索_营销网站定制

2025/7/12 17:10:04 来源:https://blog.csdn.net/m0_75138009/article/details/144313288  浏览:    关键词:潍坊住房公积金官网_潍坊网站建设方案推广_搜一搜搜索_营销网站定制
潍坊住房公积金官网_潍坊网站建设方案推广_搜一搜搜索_营销网站定制

第一步,下载插件

npm install markdown-it

第二步,使用插件渲染

<template><div v-html="htmlContent"></div>
</template><script setup>
import { ref, computed } from 'vue';
import MarkdownIt from 'markdown-it';
// 创建 MarkdownIt 实例
const md = new MarkdownIt();
// 定义一个响应式的 Markdown 文本
const markdownText = ref('# Hello, World!\nThis is a markdown paragraph in script setup.');
// 通过计算属性将 Markdown 文本转换为 HTML 内容
const htmlContent = computed(() => md.render(markdownText.value));
</script>

实际页面的应用:

<script setup>
import { inject, onMounted, ref, computed, watch } from 'vue';
import MarkdownIt from 'markdown-it';
const axios = inject('$axios');
const view = ref([]);
const getPostArticleView = async () => {const res = await axios({method: 'post',url: '/articles/view/1',});view.value = res.data.data;console.log(view);
};let hasExecuted = false;onMounted(() => {if (!hasExecuted) {getPostArticleView();hasExecuted = true;}
});const md = new MarkdownIt();
// 用于存储转换后的HTML内容,初始为空
const htmlContent = ref('');
watch(view, (newView) => {if (newView && newView.body && newView.body.content) {// 当view有有效数据时,进行Markdown转换htmlContent.value = md.render(newView.body.content);}
}, { immediate: false });
</script><template><div id="article"><div class="textSpring"><h1>{{view.title}}</h1><hr><div v-html="htmlContent"></div></div></div>
</template><style scoped>#article{padding: 20px;}.textSpring{margin: 0 auto;border: 1px solid black;border-radius: 5px;width: 60%;}
</style>

文章封面: 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com