editor.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <template>
  2. <div id="editor">
  3. <div style="border: 1px solid #ccc">
  4. <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode" />
  5. <Editor class="editor" :value="text" @input="toInput" :defaultConfig="ec" :mode="mode" @onCreated="onCreated" />
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
  11. export default {
  12. name: 'editor',
  13. props: {
  14. text: { type: String },
  15. mode: { type: String, default: 'default' }, // 或者simple
  16. toolbarConfig: { type: Object, default: () => ({}) },
  17. editorConfig: { type: Object, default: () => ({}) },
  18. },
  19. model: {
  20. prop: 'text',
  21. event: 'input',
  22. },
  23. components: { Editor, Toolbar },
  24. data: function () {
  25. return {
  26. editor: null,
  27. ec: { placeholder: '请输入内容...', ...this.editorConfig },
  28. };
  29. },
  30. created() {},
  31. methods: {
  32. onCreated(editor) {
  33. this.editor = Object.seal(editor); // 一定要用 Object.seal() ,否则会报错
  34. },
  35. toInput(e) {
  36. this.$emit('input', e);
  37. },
  38. },
  39. beforeDestroy() {
  40. const editor = this.editor;
  41. if (editor == null) return;
  42. editor.destroy(); // 组件销毁时,及时销毁编辑器
  43. },
  44. };
  45. </script>
  46. <style src="@wangeditor/editor/dist/css/style.css"></style>
  47. <style lang="less" scoped>
  48. .editor {
  49. height: 500px;
  50. overflow-y: hidden;
  51. }
  52. </style>