WXL
3 天以前 37d2ba3d2c1902202c8c7ee9485267b5a1945742
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
<template>
  <div>
    <div ref="editor" style="text-align: left;"></div>
  </div>
</template>
 
<script>
import E from 'wangeditor';
 
export default {
  name: 'WangEditor',
  data() {
    return {
      editor: null, // WangEditor 实例
    };
  },
  props: {
    content: {
      type: String,
      default: ''
    }
  },
  watch: {
    // 当父组件传入的 content 变化时,更新编辑器内容
    content(newContent) {
      if (this.editor && newContent !== this.editor.txt.html()) {
        this.editor.txt.html(newContent);
      }
    }
  },
  mounted() {
    // 初始化 WangEditor
    this.editor = new E(this.$refs.editor);
    this.editor.config.onchange = () => {
      // 编辑器内容变化时,触发 input 事件传递给父组件
      this.$emit('input', this.editor.txt.html());
    };
    // 配置菜单和其他设置
    this.editor.config.menus = [
      'head', 'bold', 'italic', 'underline', 'image', 'link', 'list', 'undo', 'redo'
    ];
    this.editor.config.zIndex = 1000;
    // 创建编辑器
    this.editor.create();
    // 设置初始内容
    if (this.content) {
      this.editor.txt.html(this.content);
    }
  },
  beforeDestroy() {
    // 销毁编辑器实例,释放资源
    if (this.editor) {
      this.editor.destroy();
    }
  }
};
</script>