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富文本编辑框的方法,通过简单的步骤实现了文本编辑与展示。希望对您在开发过程中有所帮助。如有疑问,请随时提问。

相关推荐
摇滚侠1 小时前
npm 设置了阿里云镜像,然后全局安装了 pnpm,pnpm 还需要设置阿里云镜像吗
前端·阿里云·npm
程序员清洒7 小时前
Flutter for OpenHarmony:GridView — 网格布局实现
android·前端·学习·flutter·华为
VX:Fegn08957 小时前
计算机毕业设计|基于ssm + vue超市管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·后端·课程设计
0思必得07 小时前
[Web自动化] 反爬虫
前端·爬虫·python·selenium·自动化
LawrenceLan7 小时前
Flutter 零基础入门(二十六):StatefulWidget 与状态更新 setState
开发语言·前端·flutter·dart
秋秋小事8 小时前
TypeScript 模版字面量与类型操作
前端·typescript
2401_892000528 小时前
Flutter for OpenHarmony 猫咪管家App实战 - 添加提醒实现
前端·javascript·flutter
Yolanda948 小时前
【项目经验】vue h5移动端禁止缩放
前端·javascript·vue.js
VX:Fegn08959 小时前
计算机毕业设计|基于springboot + vue酒店管理系统(源码+数据库+文档)
vue.js·spring boot·课程设计
广州华水科技9 小时前
单北斗GNSS形变监测一体机在基础设施安全中的应用与技术优势
前端