vxe-form 表单中使用渲染 wangEditor 富文本组件,配置式渲染

330 阅读1分钟

官网文档:vxeui.com

在vue使用表单的时候,经常需要用到富文本组件,wangEditor是原生的组件,要在 vue 中使用需要封装一下。

表单中使用

安装 vxe 4.x 版本,具体看官网
在 vxe-from 中,渲染任何控件,只需在 itemRender 指定控件 name 就可以了。

npm install @vxe-ui/plugin-render-wangeditor@4.0.2

在 index.html 中引入wangEditor就可以了。

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@wangeditor/editor@5.1.23/dist/css/style.css">

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@wangeditor/editor@5.1.23/dist/index.js"></script>
import { VxeUI } from 'vxe-pc-ui'
import VxeUIPluginRenderWangEditor, { WangEditor } from '@vxe-ui/plugin-render-wangeditor'
import '@vxe-ui/plugin-render-wangeditor/dist/style.css'

VXETable.use(VxeUIPluginRenderWangEditor, {
  // 自定义上传图片方法
  uploadImageMethod ({ file }) {
    const formData = new FormData()
    formData.append('file', file)
    return axios.post('/api/pub/upload/single', formData).then((res) => {
      // { url: '' }
      return {
        ...res.data
      }
    })
  },
  // 自定义上传视频方法
  uploadVideoMethod ({ file }) {
    const formData = new FormData()
    formData.append('file', file)
    return axios.post('/api/pub/upload/single', formData).then((res) => {
      // { url: '' }
      return {
        ...res.data
      }
    })
  }
})

// (可选组件)如果全局安装了,就可以在 vue 文件模板中直接使用该组件
app.use(WangEditor)

{7F816131-02A4-4E5D-B000-6D425CCEE4D4}.png

<template>
  <div>
    <vxe-form v-bind="formOptions" >
      <template #action>
        <vxe-button type="reset">重置</vxe-button>
        <vxe-button type="submit" status="primary">提交</vxe-button>
      </template>
    </vxe-form>
  </div>
</template>

<script setup>
import { reactive } from 'vue'

const formOptions = reactive({
  titleWidth: 120,
  data: {
    name: 'test1',
    remark: ''
  },
  items: [
    { field: 'name', title: '名称', span: 24, itemRender: { name: 'VxeInput' } },
    { field: 'remark', title: '富文本', span: 24, itemRender: { name: 'WangEditor' } },
    { align: 'center', span: 24, slots: { default: 'action' } }
  ]
})
</script>

试一下 ctrl + v 粘贴截图,会自动调用上传接口,转成服务端 url。

{C0C5F870-BF25-4C45-AF87-8869D412178A}.png