当前位置: 首页 > article >正文

Vue-Vue3 集成编辑器功能

1、安装依赖

编辑器插件需要安装 @wangeditor/editor@wangeditor/editor-for-vue 两个插件

npm install @wangeditor/editor --save

vue3运行如下命令安装

npm install @wangeditor/editor-for-vue@next --save

vue2运行如下命令安装

npm install @wangeditor/editor-for-vue --save

安装后可在 Vue 项目的 package.json 中查看安装依赖项
在这里插入图片描述

2、插件使用

本文章针对 Vue3 项目,在 Vue 组件中使用方式如下:

<script setup>
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import {DomEditor} from '@wangeditor/editor'
import {computed, onBeforeUnmount, ref, shallowRef} from 'vue'
import {Editor, Toolbar} from '@wangeditor/editor-for-vue'

const emit = defineEmits(["update:modelValue"])
const props = defineProps({
  modelValue: {
    type: String,
    default: ''
  },
  placeholder: {
    type: String,
    default: '请输入...'
  }
})
const inputValue = computed({
  get() {
    return props.modelValue
  },
  set(value) {
    emit("update:modelValue", value)
  }
})
// 编辑器实例,必须用 shallowRef
const editorRef = shallowRef()
const mode = ref('default')
const test = ref(false)
const editorConfig = {placeholder: props.placeholder}
// 默认工具栏配置
const toolbarConfig = {}

/** 排除菜单组,写菜单组 key 的值即可 */
toolbarConfig.excludeKeys = [

  'group-image',
  'group-video',
  'fullScreen'
]

/** 组件销毁时,也及时销毁编辑器 */
onBeforeUnmount(() => {
  const editor = editorRef.value
  if (editor == null) return
  editor.destroy()
})

/** 记录 editor 实例,重要!*/
const handleCreated = (editor) => {
  editorRef.value = editor
}
const print = () => {
  const toolbar = DomEditor.getToolbar(editorRef.value)
  const curToolbarConfig = toolbar.getConfig()
  console.log(curToolbarConfig.toolbarKeys) // 当前菜单排序和分组

  // const menu = editorRef.value.getAllMenuKeys()
  // console.log(menu)
  // console.log(editorRef.value.getMenuConfig('bgColor'))
  // console.log(valueHtml.value)
}

/** 获取HTML格式内容方法 */
const getHtml = () => {
  return editorRef.value.getHtml()
}

/** 获取原始文本内容方法 */
const getText = () => {
  return editorRef.value.getText()
}

/** 暴露方法 */
defineExpose({getHtml, getText})
</script>

<template>
  <el-button v-if="test" @click="print">打印</el-button>
  <div style="border: 1px solid #ccc">
    <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
    />
    <Editor
        style="height: 500px; overflow-y: hidden;"
        v-model="inputValue"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
    />
  </div>
</template>

<style scoped lang="less">
.el-button {
  margin: 1%;
}
</style>

效果

在这里插入图片描述

更多

插件详细使用说明请查阅官网文档:
https://www.wangeditor.com/

提示:

官网示例当中的 editor 在 Vue3 中需要通过 ref 方式获取组件实例,官网示例中组件方法调用需要加上 .value,如管网 “编辑器配置” 一说明文档中,editor.getConfig() 对应 Vue3 写法是 editor.value.getConfig()
在这里插入图片描述
在这里插入图片描述

组件实例获取方式如下:

<script setup>
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import {DomEditor} from '@wangeditor/editor'
import {computed, onBeforeUnmount, ref, shallowRef} from 'vue'
import {Editor, Toolbar} from '@wangeditor/editor-for-vue'

// 
/** 
  * 编辑器实例,必须用 shallowRef
  * 此处的 editor 对应官网的 editor 
  * 后续组件方法调用可能也是此处的 editor
  * 如:editor.getConfig() 对应 Vue3 写法是 editor.value.getConfig()
  * **/
const editor = shallowRef() 
const mode = ref('default')
const editorConfig = {placeholder: props.placeholder}
// 默认工具栏配置
const toolbarConfig = {}

const test= () => {
  const toolbar = DomEditor.getToolbar(editor.value)
  const curToolbarConfig = toolbar.getConfig()
  console.log(curToolbarConfig.toolbarKeys) // 当前菜单排序和分组

  // const menu = editor.value.getAllMenuKeys()
  // console.log(menu)
  // console.log(editor.value.getMenuConfig('bgColor'))
  // console.log(valueHtml.value)
}
</script>
<template>
    <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editor"
        :defaultConfig="toolbarConfig"
        :mode="mode"
    />
    <Editor
        style="height: 500px; overflow-y: hidden;"
        v-model="inputValue"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
    />
</template>

http://www.kler.cn/news/232781.html

相关文章:

  • 有道ai写作,突破免费限制,无限制使用
  • 数据库管理-第147期 最强Oracle监控EMCC深入使用-04(20240207)
  • 怎么把视频音乐提取成mp3?分享详细工具和方法!
  • 支持向量机
  • LeetCode力扣 面试经典150题 详细题解 (1~5) 持续更新中
  • Redis核心技术与实战【学习笔记】 - 26.Redis数分布优化(应对数据倾斜问题)
  • transformer剪枝论文汇总
  • 【Golang】定时任务Cron指南-毫秒级任务支持
  • MFC实现遍历系统进程
  • 新版MQL语言程序设计:组合模式的原理、应用及代码实现
  • 树与二叉树---数据结构
  • 医学三基答案在哪搜?4个大学生必备的搜题 #知识分享#职场发展
  • 【服务器数据恢复】服务器RAID模块硬件损坏的数据恢复案例
  • Redis——集群环境部署
  • 飞桨AI for Science流体超分FNO模型案例分享
  • django安装使用
  • 2. Maven 继承与聚合
  • 微信小程序(三十四)搜索框-带历史记录
  • Qt PCL学习(二):点云读取与保存
  • Redis Centos7 安装到启动
  • 标准库 STM32+EC11编码器+I2C ssd1306多级菜单例程
  • Spring Boot动态加载Jar包与动态配置技术探究
  • C++从零开始的打怪升级之路(day35)
  • 嵌入式软件的设计模式与方法
  • TCP和UDP相关问题(重点)(4)——4.使用TCP的协议有哪些?使用UDP的协议有哪些?
  • 第59讲订单数据下拉实现
  • 2024-02-08 思考-日记
  • MySQL组复制的介绍
  • 32. 最长有效括号
  • django中自定义视图样式