当前位置:首页 > VUE

vue 实现富文本

2026-03-08 18:07:36VUE

vue 实现富文本的方法

在 Vue 中实现富文本功能通常需要借助第三方库或组件。以下是几种常见的实现方式:

使用 vue-quill-editor

vue-quill-editor 是基于 Quill 的 Vue 富文本编辑器组件,安装简单且功能强大。

安装依赖:

npm install vue-quill-editor quill

基本使用示例:

<template>
  <quill-editor v-model="content" />
</template>

<script>
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.snow.css'

export default {
  components: { quillEditor },
  data() {
    return {
      content: ''
    }
  }
}
</script>

使用 tinymce-vue

TinyMCE 是专业级的富文本编辑器,tinymce-vue 是其官方 Vue 封装。

安装依赖:

npm install @tinymce/tinymce-vue tinymce

基本使用示例:

<template>
  <editor
    v-model="content"
    api-key="your-api-key"
    :init="editorConfig"
  />
</template>

<script>
import Editor from '@tinymce/tinymce-vue'

export default {
  components: { Editor },
  data() {
    return {
      content: '',
      editorConfig: {
        height: 500,
        plugins: 'link image code',
        toolbar: 'undo redo | bold italic | alignleft aligncenter alignright | code'
      }
    }
  }
}
</script>

使用 wangEditor

wangEditor 是轻量级的富文本编辑器,对中文支持良好。

安装依赖:

npm install wangeditor

基本使用示例:

<template>
  <div ref="editor" style="text-align:left"></div>
</template>

<script>
import E from 'wangeditor'

export default {
  data() {
    return {
      editor: null,
      content: ''
    }
  },
  mounted() {
    this.editor = new E(this.$refs.editor)
    this.editor.config.onchange = (html) => {
      this.content = html
    }
    this.editor.create()
  },
  beforeDestroy() {
    this.editor.destroy()
    this.editor = null
  }
}
</script>

使用 CKEditor 5

CKEditor 5 提供了现代化的富文本编辑体验。

安装依赖:

npm install @ckeditor/ckeditor5-vue @ckeditor/ckeditor5-build-classic

基本使用示例:

<template>
  <ckeditor :editor="editor" v-model="content" />
</template>

<script>
import CKEditor from '@ckeditor/ckeditor5-vue'
import ClassicEditor from '@ckeditor/ckeditor5-build-classic'

export default {
  components: { ckeditor: CKEditor.component },
  data() {
    return {
      editor: ClassicEditor,
      content: ''
    }
  }
}
</script>

富文本内容的安全处理

使用富文本编辑器时,需要注意 XSS 攻击防护。可以使用 DOMPurify 等库对内容进行净化:

npm install dompurify

使用示例:

import DOMPurify from 'dompurify'

const cleanHtml = DOMPurify.sanitize(dirtyHtml)

自定义富文本功能

对于特定需求,可能需要扩展或自定义富文本功能。大多数编辑器都支持插件扩展和自定义配置,具体方法可参考各编辑器的官方文档。

vue 实现富文本

性能优化建议

对于频繁更新的富文本内容,可以使用 debounce 技术减少不必要的渲染和保存操作。同时,对于大型文档,考虑使用懒加载或分块处理技术。

标签: 文本vue
分享给朋友:

相关文章

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debou…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。 使…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template>…

vue拖动实现

vue拖动实现

实现 Vue 拖动功能的方法 使用 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的原生拖放 API 实现拖动功能。通过 draggable 属性标记可拖动元素,结合 @dragsta…