Vue中使用WangEditor富文本编辑框实现文本编辑与展示

在Web开发过程中,富文本编辑器是一个非常重要的功能。本文将介绍如何在Vue项目中集成WangEditor富文本编辑框,实现文本编辑并在页面上展示。

一、WangEditor简介

WangEditor是一款基于JavaScript开发的富文本编辑器,具有轻量级、易用性等优点。它支持多种格式文本编辑,如标题、段落、列表、图片、视频等,非常适合用于博客、论坛、在线文档等场景。

二、准备工作

在开始集成WangEditor之前,请确保您的项目中已安装Vue。以下是本文所需的环境和依赖:

  1. Node.js
  2. Vue CLI
  3. WangEditor(版本:^4.7.0)

三、集成WangEditor

1、安装WangEditor

在项目根目录下,运行以下命令安装WangEditor:

bash 复制代码
npm install wangeditor --save

2、选项式API中使用WangEditor

在选项式API中,我们通常会在组件的mounted钩子中初始化WangEditor,并在methods中定义相关的方法。

javascript 复制代码
<template>
  <div>
    <div ref="editor" style="text-align:left;"></div>
    <button @click="getContent">获取内容</button>
    <div v-html="editorContent" style="border:1px solid #ccc; margin-top:10px; padding:10px;"></div>
  </div>
</template>

<script>
import E from 'wangeditor'

export default {
  name: 'WangEditor',
  data() {
    return {
      editor: null,
      editorContent: ''
    }
  },
  mounted() {
    this.initEditor()
  },
  methods: {
    initEditor() {
      this.editor = new E(this.$refs.editor)
      this.editor.customConfig.onchange = (html) => {
        this.editorContent = html
      }
      this.editor.create()
    },
    getContent() {
      console.log(this.editorContent)
    }
  }
}
</script>

3、组合式API中使用WangEditor

在组合式API中,我们使用setup函数来组织代码。setup函数是Vue 3中的新特性,它运行在组件的生命周期之前,是组件的入口。

javascript 复制代码
<template>
  <div>
    <div ref="editorRef" style="text-align:left;"></div>
    <button @click="getContent">获取内容</button>
    <div v-html="editorContent" style="border:1px solid #ccc; margin-top:10px; padding:10px;"></div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import E from 'wangeditor'

export default {
  name: 'WangEditor',
  setup() {
    const editorRef = ref(null)
    const editorContent = ref('')

    const initEditor = () => {
      const editor = new E(editorRef.value)
      editor.customConfig.onchange = (html) => {
        editorContent.value = html
      }
      editor.create()
    }

    const getContent = () => {
      console.log(editorContent.value)
    }

    onMounted(() => {
      initEditor()
    })

    return {
      editorRef,
      editorContent,
      getContent
    }
  }
}
</script>

在组合式API中,我们使用ref来定义响应式数据,并使用onMounted生命周期钩子来初始化编辑器。setup函数返回的对象中的属性和方法都可以在模板中直接使用。

本文介绍了在Vue项目中集成WangEditor富文本编辑框的方法,通过简单的步骤实现了文本编辑与展示。希望对您在开发过程中有所帮助。如有疑问,请随时提问。

相关推荐
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
ew452182 小时前
ElementUI表格表头自定义添加checkbox,点击选中样式不生效
前端·javascript·elementui
suibian52352 小时前
AI时代:前端开发的职业发展路径拓宽
前端·人工智能
画月的亮2 小时前
element-ui 使用过程中遇到的一些问题及解决方法
javascript·vue.js·ui
Moon.92 小时前
el-table的hasChildren不生效?子级没数据还显示箭头号?树形数据无法展开和收缩
前端·vue.js·html
m0_526119402 小时前
点击el-dialog弹框跳到其他页面浏览器的滚动条消失了多了 el-popup-parent--hidden
javascript·vue.js·elementui
垚垚 Securify 前沿站2 小时前
深入了解 AppScan 工具的使用:筑牢 Web 应用安全防线
运维·前端·网络·安全·web安全·系统安全
工业甲酰苯胺5 小时前
Vue3 基础概念与环境搭建
前端·javascript·vue.js
lyj1689975 小时前
el-tree选中数据重组成树
javascript·vue.js·elementui
mosquito_lover16 小时前
怎么把pyqt界面做的像web一样漂亮
前端·python·pyqt