Vue中使用 vditor 使用详细步骤,代码以及注释,效果图

使用 Vditor 在 Vue 中编辑并展示 Markdown 文本的步骤如下:

1. 安装 Vditor:使用 npm 安装 Vditor 插件:

npm install --save vditor

2. 导入 Vditor:在需要使用 Vditor 的 Vue 组件中导入 Vditor 插件:

import Vditor from 'vditor';

3. 创建 Vditor 实例:在 Vue 组件的 created 生命周期钩子函数中创建 Vditor 实例,并将其挂载到 Vue 实例的 data 中:

created() {
   
  this.vditor = new Vditor('vditorContainer');
}

4. 添加 Vditor 容器:在 Vue 组件的模板中添加一个与 Vditor 实例绑定的容器:

<template>
  <div id="vditorContainer"></div>
</template>

5. 设置 Vditor 配置:根据需要设置 Vditor 的配置项,例如编辑器模式(编辑或预览)、主题、语言、提示功能等。可以在 created 生命周期钩子函数中调用 Vditor 的 setOption 方法设置配置项:

created() {
   
  this.vditor = new Vditor('vditorContainer');
  this.vditor.setOption({
   
    mode: 'wysiwyg',
    theme: 'classic',
    lang: 'zh_CN',
    hint: {
   
      emoji: {
   
        '+1': '👍',
        '-1': '👎',
        '100': '💯',
        // 其他自定义 emoji
      },
      emojiTail: ' ',
      at: ['user1', 'user2', 'user3'],
      // 其他自定义提示
    }
  });
}

6. 获取 Markdown 文本:通过 Vditor 实例的 getValue 方法获取编辑器中的 Markdown 文本。

methods: {
   
  getMarkdown() {
   
    const markdown = this.vditor.getValue();
    console.log(markdown);
  }
}

7. 展示 Markdown 文本:将获取到的 Markdown 文本转换为 HTML 并在页面中展示。可以使用第三方的 Markdown 解析库,如 markdown-it。

<template>
  <div>
    <div id="vditorContainer"></div>
    <div v-html="html"></div>
  </div>
</template>

<script>
import Vditor from 'vditor';
import MarkdownIt from 'markdown-it';

export default {
   
  data() {
   
    return {
   
      vditor: null,
      markdown: '',
      html: ''
    };
  },
  created() {
   
    this.vditor = new Vditor('vditorContainer');
  },
  methods: {
   
    getMarkdown() {
   
      this.markdown = this.vditor.getValue();
      const md = new MarkdownIt();
      this.html = md.render(this.markdown);
    }
  }
}
</script>

最近更新

  1. TCP协议是安全的吗?

    2023-12-21 15:12:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-21 15:12:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-21 15:12:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-21 15:12:02       20 阅读

热门阅读

  1. 改变图片亮度的 Python 实现算法

    2023-12-21 15:12:02       45 阅读
  2. 【AI-1】卷积神经网络

    2023-12-21 15:12:02       28 阅读
  3. 创建局域网git裸仓库

    2023-12-21 15:12:02       35 阅读
  4. Shell脚本应用(二)

    2023-12-21 15:12:02       37 阅读
  5. Docker Swarm集群的深度总结

    2023-12-21 15:12:02       36 阅读
  6. c# 使用OpenCV

    2023-12-21 15:12:02       37 阅读
  7. Vue3使用 xx UI解决布局高度自适应

    2023-12-21 15:12:02       43 阅读
  8. MySQL8_rpm方式安装过程中常见问题处理

    2023-12-21 15:12:02       35 阅读
  9. 拾玖[19],拟合线,函数FitLineContourXld

    2023-12-21 15:12:02       40 阅读
  10. PHP函数里面写JQ CSS HTML的写法案例

    2023-12-21 15:12:02       28 阅读