v-md-editor 是基于 Vue 开发的 markdown 编辑器组件
# 使用 npm
npm i @kangc/v-md-editor@next -S
# 使用 yarn
yarn add @kangc/v-md-editor@next
首先在项目的main.js中进行挂载,然后再去页面中使用
import { createApp } from 'vue';
import VMdEditor from '@kangc/v-md-editor';
import '@kangc/v-md-editor/lib/style/base-editor.css';
import githubTheme from '@kangc/v-md-editor/lib/theme/github.js';
import '@kangc/v-md-editor/lib/theme/style/github.css';
// highlightjs
import hljs from 'highlight.js';
VMdEditor.use(githubTheme, {
Hljs: hljs,
});
const app = createApp(/*...*/);
app.use(VMdEditor);
页面使用
<template>
<v-md-editor v-model="text" height="400px">v-md-editor>
template>
<script setup>
import {ref} from 'vue'
const text = ref("")
script>
import VMdPreview from '@kangc/v-md-editor/lib/preview';
import '@kangc/v-md-editor/lib/style/preview.css'
VMdPreview.use(githubTheme, {
Hljs: hljs,
});
const app = createApp(/*...*/);
app.use(VMdPreview);
<template>
<v-md-preview :text="content">v-md-preview>
template>
<script setup>
import {ref} from 'vue'
const content =ref()
script>
markdown 编辑器界面

markdown 预览界面

import './assets/main.css'
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
// markdown编辑器
import VMdEditor from '@kangc/v-md-editor';
import '@kangc/v-md-editor/lib/style/base-editor.css';
import githubTheme from '@kangc/v-md-editor/lib/theme/github.js';
import '@kangc/v-md-editor/lib/theme/style/github.css';
import hljs from 'highlight.js'; // highlightjs
VMdEditor.use(githubTheme, {
Hljs: hljs,
});
// markdown 预览 markdown解析需要的配置项目
import VMdPreview from '@kangc/v-md-editor/lib/preview';
// import '@kangc/v-md-editor/lib/style/preview.css'
// import githubTheme from '@kangc/v-md-editor/lib/theme/github';
import '@kangc/v-md-editor/lib/theme/style/github.css';
// import hljs from 'highlight.js';
VMdPreview.use(githubTheme, {
Hljs: hljs,
});
const app = createApp(App)
app.use(router)
app.use(VMdEditor)
app.use(VMdPreview)
app.mount('#app')
<script setup>
import { ref } from 'vue'
import { RouterLink } from 'vue-router'
const text = ref("")
script>
<template>
<main>
<div style="width:80vw;">
<h3>markdown编辑器h3> <RouterLink to="/read">markdown预览测试RouterLink>
<v-md-editor v-model="text" height="700px" >v-md-editor>
div>
main>
template>
<script setup>
import { ref } from 'vue'
const contentA = ref()
const readText = ()=>{
// 请求的是我自己的接口地址,后端代码逻辑就是存到数据库中的一个text文件,然后下面完整的代码中,我提供了我的markdown文本
fetch('http://robin.com/Article/18')
.then(response => response.text())
.then(data => {
contentA.value = JSON.parse(data).content
//console.log(contentA.value)
})
.catch((error) => console.error('Error:', error));
}
readText()
script>
<template>
<div class="about">
<div>
<v-md-preview :text="contentA">v-md-preview>
div>
div>
template>
<style>
@media (min-width: 1024px) {
.about {
min-height: 100vh;
min-width: 1024px;
margin: 0 auto;
display: flex;
align-items: center;
}
}
style>
链接: https://pan.baidu.com/s/1kzCxIt-oXdsKCKgBW91gOQ 提取码: zk3w 复制这段内容后打开百度网盘手机App,操作更方便哦