vue3使用富文本编辑器wangEditor-v5(未使用composition api写法)

本文介绍如何使用WangEditor富文本编辑器及其Vue组件库创建一个可直接与Vue应用程序交互的封装组件。通过具体步骤说明如何安装核心库,并提供代码示例展示如何设置配置项以实现自定义功能。

效果

在这里插入图片描述

安装

安装核心库和其vue组件库

yarn add @wangeditor/editor
yarn add @wangeditor/editor-for-vue@next

使用v-model封装富文本组件editor.vue

<template>
  <div class="editor-box">
    <Toolbar class="toolbar" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode" ref="t" />
    <Editor class="editor" v-model="html" :defaultConfig="editorConfig" :mode="mode" @onCreated="onCreated" />
  </div>
</template>
<script>
import "@wangeditor/editor/dist/css/style.css"
import { Editor, Toolbar } from "@wangeditor/editor-for-vue"
import { DomEditor } from "@wangeditor/editor"
export default {
  components: { Editor, Toolbar },
  data() {
    return {
      editor: null,
      toolbarConfig: {
        // 不需要的工具栏选项,这里去除了emoji表情
        excludeKeys: ["emotion"],
      },
      editorConfig: {
        placeholder: "请输入内容...",
        // 这里可以用于自定义emoji表情
        MENU_CONF: {
          emotion: {
            // emotions: "😀 😃 😄 😁 😆 😅 😂 🤣 😊 😇 🙂 🙃 😉".split(" "), // 数组
          },
        },
      },
      mode: "default",
    }
  },
  props: {
    modelValue: String,
  },
  computed: {
    html: {
      get() {
        return this.modelValue
      },
      set(v) {
        this.$emit("update:modelValue", v)
      },
    },
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
    },
  },
  mounted() {
    // 获取工具栏的所有key
    setTimeout(() => {
      const toolbar1 = DomEditor.getToolbar(this.editor)
      console.log("keys", toolbar1.getConfig())
    }, 100)
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时,及时销毁编辑器
  },
}
</script>
<style lang="scss" scoped>
.w-e-full-screen-container {
  z-index: 9999;
}
.editor-box {
  border: 1px solid #ccc;
}
.toolbar {
  border-bottom: 1px solid #ccc;
}
.editor {
  height: 500px !important;
}
</style>

使用组件

<template>
  <div class="test">
    {{ text }}
    <editor v-model="text"></editor>
  </div>
</template>
<script>
export default {
  data() {
    return {
      text: "",
    }
  },
  watch: {},
  methods: {},
  mounted() {
    // 模拟接口请求
    setTimeout(() => {
      this.text = "123"
    }, 1000)
  },
}
</script>

评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值