【vue3】wangEditor 5在vue3中的使用

实现效果:

将wangEditor富文本编辑器进行封装,父组件可以使用子组件富文本编辑器中的数据用于api对接。

1.安装


官网:https://www.wangeditor.com

# Vue2 安装
yarn add @wangeditor/editor-for-vue
# 或者 npm install @wangeditor/editor-for-vue --save
 
# Vue3 安装
yarn add @wangeditor/editor-for-vue@next
# 或者 npm install @wangeditor/editor-for-vue@next --save

2.创建编辑器vue文件

commonEdit.vue

<template>
  <div style="border: 1px solid #ccc">
    <Toolbar
      :editor="editorRef"
      :defaultConfig="toolbarConfig"
      :mode="mode"
      style="border-bottom: 1px solid #ccc"
    />
    <Editor
      v-model="valueHtml"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="handleCreated"
      @onChange="handleChange"
      style="height: 500px; overflow-y: hidden;"
    />
  </div>
</template>
 
<script setup lang="ts">
 
  import { watch,onBeforeUnmount,nextTick, ref, shallowRef, onMounted,onBeforeMount } from 'vue'
  //@ts-ignore
  import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  import '@wangeditor/editor/dist/css/style.css' // 引入 css
 
  // Props:使用属性,子组件接收父组件传递的内容
  const props = defineProps({
    content: { type: String, default: '' }
  })
 
  // Emits:使用事件,将子组件内容传递给父组件。父组件使用 update(content: string) 
  const emit = defineEmits<{ (e: 'update', content: string): void }>()
 
  const mode = ref('default')
 
  // 编辑器实例,必须用 shallowRef
  const editorRef = shallowRef()
 
  // 内容 HTML
  const valueHtml = ref('')
 
  const toolbarConfig = {}
 
  const editorConfig = { 
    placeholder: '请输入内容...' ,
    MENU_CONF:{} as any
  }
 
  // 上传图片配置
  editorConfig.MENU_CONF['uploadImage'] = {
    // form-data fieldName ,默认值 'wangeditor-uploaded-image'。传给后端接口的参数名,重要!
    fieldName: 'file',
    server: 'http://localhost:8080/files/wangEditorUpload'
  }
 
  const handleCreated = (editor:any) => {
    editorRef.value = editor // 记录 editor 实例,重要!
  }
 
  const handleChange = () => { 
    valueHtml.value  = editorRef.value.getHtml()
    emit('update', valueHtml.value) 
  }
 
  // 监听 props 变化,监听父组件传来的content
  watch(() => props.content, (newVal:string) => {
      nextTick(() => {
        if (editorRef.value) {
          // console.log(" 当前编辑器的状态:", editorRef.value); 
 
          // 富文本编辑器按 html 格式回显
          editorRef.value.setHtml(newVal)
          valueHtml.value = newVal
        }
      })
    }
  )
 
  onMounted(async() => {
    await nextTick(); // 延迟渲染,确保 DOM 更新完成
    if(props.content) {
      valueHtml.value = props.content 
    }
  })
 
  // 组件销毁时,也及时销毁编辑器
  onBeforeUnmount(() => {
      const editor = editorRef.value
      if (editor == null) return
      editor.destroy()
  })
 
</script>

3.父组件中引用

<template>
  <el-form :model="form" label-width="auto" style="max-width: 800px">
    <el-form-item label="文章标题">
      <el-input v-model="form.title" />
    </el-form-item>
	<el-form-item label="文章内容">
	  <commonEdit :content="form.html" @update="update"/>
	</el-form-item>
   
    <el-form-item label="文章简介">
      <el-input v-model="form.desc" type="textarea" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit(form)">提交</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import commonEdit from '../components/commonEdit.vue';
import { reactive } from 'vue'
import { addArticle } from '../api';
import router from '../router';
// do not use same name with ref
const form = reactive({
  title: '',
  desc: '',
  html:'',
})

const onSubmit = (data:any) => {
	console.log(data);
  addArticle(data)
  router.push('/article')
}

// 更新富文本编辑器内容
  const update = (content:string) => {

    form.html=content
  };

</script>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue3 wangeditor5 是基于 Vue3 开发的富文本编辑器,拥有许多强大的功能和特性。 首先,Vue3 是 Vue.js 的新版本,相较于旧版本,它在性能、体积和开发体验等方面都进行了优化和改进。因此,使用 Vue3 开发的 wangeditor5 在性能和交互体验上都更加流畅和高效。 其次,wangeditor5 是一个功能强大的富文本编辑器,它提供了一系列丰富的编辑功能,例如文本格式设置(字体、大小、颜色等),段落样式(标题、正文、引用等),插入图片和视频,表格编辑,插入链接和代码等。这些功能使得用户能够轻松地创建复杂的富文本内容,并对内容进行灵活的编辑和样式设置。 此外,wangeditor5 还提供了丰富的插件和扩展接口,用户可以根据自己的需求自定义编辑器的功能和样式。例如,可以通过插件集成 Markdown 编辑功能,通过扩展接口实现自定义的插入工具等。这使得 wangeditor5 在满足基本编辑需求的同时,也具备了良好的可扩展性和灵活性。 最后,wangeditor5 还支持响应式布局,并且支持移动设备的操作和触摸事件。这使得用户能够在手机或平板上轻松地使用编辑器,方便地进行编辑和修改。 综上所述,vue3 wangeditor5 是一个功能强大、性能优化、易于扩展和适合移动设备的富文本编辑器。无论是用于个人博客、管理系统还是社交平台,它都能满足用户的编辑需求,并且提供良好的交互体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值