Vue3——v-md-editor(markDown编辑器)使用教程

简介: v-md-editor(markDown编辑器)使用教程

Vue3——v-md-editor安装使用教程

安装

# 使用 npm
npm i @kangc/v-md-editor -S

EditorMarkdown.vue页面用来封装此编辑器组件

Test.vue作为接受EditorMarkdown.vue的父组件,当测试页使用

路由部分要放入test.vue

main.js部分全局引入组件

import EditorMarkdown from '@/components/EditorMarkdown.vue';
app.component('EditorMarkdown',EditorMarkdown)

EditorMarkdown页面引入

<!-- author: Mr.J -->
<!-- date: 2023-03-29 15:01:56 -->
<!-- description: Vue3+JS代码块模板 -->
<template>
    <!-- 这里需要注意一下,官网上给的引入方式需要改一下,应该是没有更新导致的;还有一点就是modelValue需要添加括号,否则找不到这个值 -->
   <VueMarkdownEditor v-model="(modelValue)" :height="height+'px'"></VueMarkdownEditor>
</template>
<script setup>
import VueMarkdownEditor from "@kangc/v-md-editor";
import "@kangc/v-md-editor/lib/style/base-editor.css";
import vuepressTheme from "@kangc/v-md-editor/lib/theme/vuepress.js";
import "@kangc/v-md-editor/lib/theme/style/vuepress.css";
import Prism from "prismjs";
VueMarkdownEditor.use(vuepressTheme, {
  Prism,
});
const props = defineProps({
  modelValue: {
    type: String,
    default: "",
  },
  height: {
    type: Number,
    default: 500,
  },
});
</script>
<style>
</style>

官网给出的教程图片如下:

1a7e19a6b7af452881e6c6cb10e5049e.png

这里会发现有些误差,调整一下即可

还有一点要注意的是如果直接将props中的值给到v-model需要加上括号,下面是代码片段

Test.vue

<!-- author: Mr.J -->
<!-- date: 2023-03-29 15:03:56 -->
<!-- description: Vue3+JS代码块模板 -->
<template>
    <editor-markdown></editor-markdown>
</template>
<script setup>
import { ref,reactive,onMounted } from "vue";
</script>
<style>
</style>

图片上传

<VueMarkdownEditor v-model="(modelValue)" :disabled-menus="[]" :include-level="[1,2,3,4,5,6]"
    @upload-image="handleUploadImage"></VueMarkdownEditor>
const handleUploadImage = async(event, insertImage, files) => {
    console.log(files);
    // 这里做自定义图片上传
    let result = await proxy.Request({
        url:'/file/Image',
        dataType:'file',
        params:{
            file:files[0],
            type:1,
        }
    })
    if (!result) {
        return
    }
    const url = proxy.globaInfo.imageUrl+ result.data.fileName
    insertImage({
        url:url,
        desc: '博客图片',
        // width: 'auto',
        // height: 'auto',
      });
};

测试双向绑定

<VueMarkdownEditor v-model="(modelValue)"  @change="change" :height="height+'px'"></VueMarkdownEditor>
// 子传父
const emit = defineEmits()
const change=(markdownContent,htmlContent)=>{
    emit('update:modelValue',markdownContent)
    emit('htmlContent',htmlContent)
}

test.vue进行测试

<template>
    <editor-markdown v-model="markdownContent"></editor-markdown>
    {{ markdownContent }}
</template>
<script setup>
import { ref,reactive,onMounted } from "vue";
const markdownContent = ref('# test')
</script>
<style>
</style>

效果如下:

f6ccbbc4675d486784621c0a8cfd582c.gif

这样一个组件就已经封装好了

以下附上完整代码:

EditorMarkdown.vue

<!-- author: Mr.J -->
<!-- date: 2023-03-29 15:01:56 -->
<!-- description: Vue3+JS代码块模板 -->
<template>
   <!-- :disabled-menus="[]"把禁用的title放在此数组中
        :include-level="[1,2,3,4,5,6]" 点击目录导航的层级
        @upload-image="handleUploadImage"图片上传
        @change="change"双向绑定效果
     -->
  <VueMarkdownEditor v-model="(modelValue)" :disabled-menus="[]" :include-level="[1,2,3,4,5,6]"
    @upload-image="handleUploadImage" @change="change" :height="height+'px'"></VueMarkdownEditor>
</template>
<script setup>
import VueMarkdownEditor from "@kangc/v-md-editor";
import "@kangc/v-md-editor/lib/style/base-editor.css";
import vuepressTheme from "@kangc/v-md-editor/lib/theme/vuepress.js";
import "@kangc/v-md-editor/lib/theme/style/vuepress.css";
import Prism from "prismjs";
import { getCurrentInstance } from "vue";
const {proxy} = getCurrentInstance()
VueMarkdownEditor.use(vuepressTheme, {
  Prism,
});
const props = defineProps({
  modelValue: {
    type: String,
    default: "",
  },
  height: {
    type: Number,
    default: 500,
  },
});
const emit = defineEmits()
const change=(markdownContent,htmlContent)=>{
    emit('update:modelValue',markdownContent)
    emit('htmlContent',htmlContent)
}
const handleUploadImage = async(event, insertImage, files) => {
    console.log(files);
    // 这里做自定义图片上传
    let result = await proxy.Request({
        url:'/file/uploadImage',
        dataType:'file',
        params:{
            file:files[0],
            type:1,
        }
    })
    if (!result) {
        return
    }
    const url = proxy.globaInfo.imageUrl+ result.data.fileName
    insertImage({
        url:url,
        desc: '博客图片',
        // width: 'auto',
        // height: 'auto',
      });
};
</script>
<style>
</style>

Test.vue

<!-- author: Mr.J -->
<!-- date: 2023-03-29 15:03:56 -->
<!-- description: Vue3+JS代码块模板 -->
<template>
    <editor-markdown v-model="markdownContent"></editor-markdown>
    {{ markdownContent }}
</template>
<script setup>
import { ref,reactive,onMounted } from "vue";
const markdownContent = ref()
</script>
<style>
</style>
相关文章
|
1天前
|
JSON 搜索推荐 定位技术
地图主题编辑器使用教程
地图主题编辑器使用教程
6 0
|
4天前
|
Linux C++ 开发者
几款主流好用的markdown编辑器介绍
几款主流好用的markdown编辑器介绍
13 0
|
19天前
Markdown编辑器--冷门实用知识总结
Markdown编辑器--冷门实用知识总结
|
28天前
|
存储 移动开发 编解码
基于HTML5开发的Markdown在线编辑器
Markdown是一种轻量级标记语言,以其简洁易读的格式而备受程序员和作者们的青睐。随着互联网的发展,越来越多的在线Markdown编辑器应运而生,为用户提供了更加便捷、高效的写作和编辑环境。本文将探讨基于HTML5开发的Markdown在线编辑器的设计原理、功能特点以及技术优势。
59 4
|
29天前
|
开发工具 Linux 程序员
20. 【Linux教程】emacs 编辑器
20. 【Linux教程】emacs 编辑器
14 0
|
29天前
|
Linux 开发工具 Unix
19. 【Linux教程】nano 编辑器
19. 【Linux教程】nano 编辑器
21 0
19. 【Linux教程】nano 编辑器
|
29天前
|
开发工具 Linux Unix
18. 【Linux教程】vim 编辑器
18. 【Linux教程】vim 编辑器
23 0
|
2月前
|
Web App开发 存储 机器学习/深度学习
欢迎使用CSDN-markdown编辑器
欢迎使用CSDN-markdown编辑器
27 0
|
2月前
|
存储 移动开发 编解码
基于HTML5开发的Markdown在线编辑器
Markdown是一种轻量级标记语言,以其简洁易读的格式而备受程序员和作者们的青睐。随着互联网的发展,越来越多的在线Markdown编辑器应运而生,为用户提供了更加便捷、高效的写作和编辑环境。本文将探讨基于HTML5开发的Markdown在线编辑器的设计原理、功能特点以及技术优势。
37 1
基于HTML5开发的Markdown在线编辑器
|
2月前
|
Web App开发 移动开发 搜索推荐
常见的Markdown在线编辑器
在线Markdown编辑器提供了更加稳定和流畅的用户体验。用户无需下载安装任何软件,只需打开浏览器,即可在任何设备上轻松使用这款编辑器,实现随时随地的写作。基于HTML5的在线Markdown编辑器可实现即时的编辑和预览功能
39 2