Vue3中使用富文本编辑器的示例详解

 更新时间:2023年10月20日 13:53:33   作者:会说法语的猪  
这篇文章主要为大家详细介绍了Vue3中使用富文本编辑器的相关知识,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以参考一下

在vue3中我们可以使用@wangeditor/editor、@wangeditor/editor-for-vue来实现其功能

npm地址:https://www.npmjs.com/package/@wangeditor/editor-for-vue/v/5.1.12?activeTab=readme

官网:Editor 

1. 安装

pnpm add @wangeditor/editor
# 或者 npm install @wangeditor/editor --save
 
pnpm add @wangeditor/editor-for-vue@next
# 或者 npm install @wangeditor/editor-for-vue@next --save

2. 组件封装

@/comps/Editor/index.vue 

首先为了能让vue认识@wangeditor/editor-for-vue库、我们可以在.d.ts中声明一下即可

// 声明外部 npm 插件模块
declare module '@wangeditor/editor-for-vue';
<template>
  <div class="Wft-Editor flex flex-col">
    <Toolbar
      class="default-border"
      :editor="editorRef"
      :mode="mode"
    />
    <Editor
      class="flex-1 overflow-y-auto"
      v-model="valueHtml"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="handleCreated"
      @onChange="onChange"
    />
  </div>
</template>
<script setup lang="ts">
import '@wangeditor/editor/dist/css/style.css'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import type { IDomEditor } from '@wangeditor/editor'
import { onBeforeUnmount, shallowRef, computed, watch } from 'vue'
 
type TProps = {
  mode?: string,
  valueHtml?: string,
  placeholder?: string,
  disable?: boolean
}
const props = withDefaults(defineProps<TProps>(), {
  mode: 'default', // or 'simple'
  valueHtml: '',
  placeholder: '请输入内容...',
  disable: false
})
type TEmits = {
  (e: 'update:valueHtml', params: string): void
  (e: 'update:valueText', params: string): void
  (e: 'onChange', params: IDomEditor): void
}
const emits = defineEmits<TEmits>()
 
const editorRef = shallowRef()
 
const valueHtml = computed({
  get() {
    return props.valueHtml
  },
  set(valueHtml) {
    emits('update:valueHtml', valueHtml)
  }
})
 
watch(() => props.disable, bool => {
  if(!editorRef.value) return
  bool ? (editorRef.value as IDomEditor).disable() : (editorRef.value as IDomEditor).enable()
}, { deep: true })
 
const editorConfig = computed(() => {
  return { placeholder: props.placeholder }
})
 
// 记录 editor 实例,重要!
const handleCreated = (editor: IDomEditor) => {
  editorRef.value = editor
}
 
// editor 改变
const onChange = (editor: IDomEditor) => {
  emits('onChange', editor)
}
 
// 销毁编辑器
onBeforeUnmount(() => {
  if(!editorRef.value) return
  editorRef.value.destroy()
})
 
function getHtml() {
  return (editorRef.value as IDomEditor).getHtml()
}
function getText() {
  return (editorRef.value as IDomEditor).getText()
}
defineExpose({ getHtml, getText })
</script>
<style scoped>
.Wft-Editor {
  width: 100%;
  height: 100%;
}
.flex {
  display: flex;
}
 
.flex-col {
  flex-direction: column;
}
 
.default-border {
  border-bottom: 1px solid #ccc;
}
 
.flex-1 {
  flex: 1;
}
 
.overflow-y-auto {
  overflow-x: hidden;
  overflow-y: auto;
}
</style>

3. 使用 

<template>
  <div class="wel">
    <div class="btn">
      <button @click="submit">提交</button>
      <button @click="getHtml">获取HTML</button>
      <button @click="getText">获取TEXT</button>
      <button @click="editorDisable = !editorDisable">{{ editorDisable ? '启用' : '禁用' }}</button>
    </div>
    <div class="editor-container">
      <Editor
        ref="EditorRef"
        :disable="editorDisable"
        v-model:value-html="editorValue"
        @on-change="editorChange"
      />
    </div>
  </div>
</template>
<script setup lang="ts">
import Editor from '@/comps/Editor/index.vue'
import { onMounted, ref, shallowRef } from 'vue'
import type { IDomEditor } from '@wangeditor/editor'
 
let editorValue = ref('') // 提交的数据
let editorDisable = ref(false)
let EditorRef = shallowRef<InstanceType<typeof Editor>>()
 
onMounted(() => {
  setTimeout(() => {
    editorValue.value = '<h1>回显测试</h1>'
  }, 3000)
})
 
const submit = () => {
  console.log(editorValue.value)
}
const getHtml = () => {
  console.log(EditorRef.value?.getHtml())
}
const getText = () => {
  console.log(EditorRef.value?.getText())
}
const editorChange = (editor: IDomEditor) => {
  console.log(editor.getHtml())
  console.log(editor.getText())
}
</script>
<style scoped>
.wel {
  width: 100%;
  height: 100%;
}
.btn {
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
}
.btn button {
  margin-left: 15px;
}
.editor-container {
  width: 100%;
  height: calc(100% - 40px);
}
</style>

4. 效果 

到此这篇关于Vue3中使用富文本编辑器的示例详解的文章就介绍到这了,更多相关Vue3富文本编辑器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Element-ui DatePicker显示周数的方法示例

    Element-ui DatePicker显示周数的方法示例

    这篇文章主要介绍了Element-ui DatePicker显示周数的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • 基于vuejs+webpack的日期选择插件

    基于vuejs+webpack的日期选择插件

    这篇文章主要为大家详细介绍了基于vuejs+webpack的日期选择插件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • vue通过接口直接下载java生成好的Excel表格案例

    vue通过接口直接下载java生成好的Excel表格案例

    这篇文章主要介绍了vue通过接口直接下载java生成好的Excel表格案例
    2020-10-10
  • vue+golang实现上传微信头像功能

    vue+golang实现上传微信头像功能

    这篇文章主要介绍了vue+golang实现上传微信头像功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2023-10-10
  • Vue批量文件上传并发的踩坑指南

    Vue批量文件上传并发的踩坑指南

    本文详细探讨了Vue批量文件上传中的并发问题,并提出了四种解决方案:基础版(固定并发数)、进阶版、高级版(和极简版,每种方案针对不同场景和需求进行了优化,旨在平衡上传效率与服务器压力,同时提高上传的稳定性和成功率,需要的朋友可以参考下
    2026-04-04
  • vue中slot(插槽)的介绍与使用

    vue中slot(插槽)的介绍与使用

    这篇文章主要给大家介绍了关于vue中slot(插槽)的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用vue具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • Vue.js弹出模态框组件开发的示例代码

    Vue.js弹出模态框组件开发的示例代码

    本篇文章主要介绍了Vue.js弹出模态框组件开发的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • Vue引入Stylus知识点总结

    Vue引入Stylus知识点总结

    在本篇文章里小编给各位整理的是一篇关于Vue引入Stylus知识点总结内容,有需要的朋友们可以学习参考下。
    2020-01-01
  • vue中使用echarts并根据选择条件动态展示echarts图表

    vue中使用echarts并根据选择条件动态展示echarts图表

    虽然老早就看过很多echarts的例子, 但自己接触的项目中一直都没有真正用到过,直到最近才开始真正使用,下面这篇文章主要给大家介绍了关于vue中使用echarts并根据选择条件动态展示echarts图表的相关资料,需要的朋友可以参考下
    2023-12-12
  • 详解基于Vue,Nginx的前后端不分离部署教程

    详解基于Vue,Nginx的前后端不分离部署教程

    这篇文章主要介绍了详解基于Vue,Nginx的前后端不分离部署教程,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12

最新评论