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


    效果

    在这里插入图片描述

    安装

    安装核心库和其vue组件库

    yarn add @wangeditor/editor
    yarn add @wangeditor/editor-for-vue@next
    
    • 1
    • 2

    使用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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    使用组件

    <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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    深度学习模型试跑(十四):Bytetrack(vs2019 训练+trt推理部署)
    Codeforces Round #836 (Div. 2)
    【SA8295P 源码分析】125 - MAX96712 解串器 start_stream、stop_stream 寄存器配置 过程详细解析
    Hi3519AV100通过ORTP库实现局域网图传和VLC实时预览
    贪心算法之最优装载问题
    Typescript基本类型
    【嵌入式开发 Linux 常用命令系列 8 --代码格式修改工具 astyle】
    ELK 企业级日志分析系统
    Python和Excel的完美结合:常用操作汇总(案例详析)
    k8s 集群使用 haproxy+keepalived+nginx 实现k8s集群负载均衡
  • 原文地址:https://blog.csdn.net/qq_42611074/article/details/127986301