wangEditor 移除表情包菜单项的配置步骤
1. 确认表情包菜单项的 Key
默认 Key 为 emotion:根据工具栏默认配置,表情包菜单项的 Key 为 emotion。
// 通过代码动态获取所有菜单项 Key(可选)
const editorRef = shallowRef()
const handleCreated = (editor) => {
const allMenuKeys = editor.getAllMenuKeys() // 包含 'emotion'
console.log('所有菜单项 Key:', allMenuKeys)
}
2. 配置 excludeKeys 排除表情包
在 Toolbar 组件中传递配置:确保 excludeKeys 绑定到 Toolbar 组件的 defaultConfig 属性。
<template>
<Toolbar :defaultConfig="toolbarConfig" />
<Editor v-model="valueHtml" :defaultConfig="editorConfig" />
</template>
<script setup>
import { IToolbarConfig } from '@wangeditor/editor'
// 工具栏配置
const toolbarConfig: Partial<IToolbarConfig> = {
excludeKeys: ['emotion'] // 移除表情包
}
</script>
3. 验证配置生效
检查 DOM 结构:使用浏览器开发者工具查看工具栏 DOM,确认 emotion 相关元素已移除。
打印当前工具栏配置:
const handleCreated = (editor) => {
const toolbar = editor.getToolbar()
console.log('当前生效的工具栏配置:', toolbar.getConfig().toolbarKeys)
}
注意事项
版本一致性: 确保 @wangeditor/editor-for-vue 和 @wangeditor/editor 主版本均为 v5.x。
动态修改需重建实例: 若运行时需更新配置,需先销毁旧实例再创建新实例。
服务端渲染兼容: 在 Nuxt3 等 SSR 框架中,需使用 包裹编辑器组件。