下载:
npm install @wangeditor/editor @wangeditor/editor-for-vue@next
使用:
<template>
<div class="tool-bar">
<!-- 工具栏 -->
<Toolbar
class="tool-style"
:editor="editorRef"
:defaultConfig="toolbarConfig"
/>
<!-- 编辑器 -->
<Editor
class="content"
v-model="valueHtml"
:defaultConfig="editorConfig"
@onCreated="handleCreated"
/>
</div>
</template>
<script setup>
import { onBeforeUnmount, ref, shallowRef} from 'vue'
import '@wangeditor/editor/dist/css/style.css'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
const editorRef = shallowRef()
const toolbarConfig= ref()
// 内容 HTML
const valueHtml = ref('<p></p>')
// 编辑器配置
const editorConfig = {
placeholder: '请输入内容...',
MENU_CONF: { /* 菜单配置,下文解释 */ }
}
const handleCreated = (editor) => {
editorRef.value = editor // 记录 editor 实例,重要!
}
// 组件销毁时,及时销毁编辑器
onBeforeUnmount(() => {
const editor = editorRef.value
if (editor == null) return
editor.destroy()
})
</script>
组件效果: