当前位置:   article > 正文

Vue3使用WangEditor编辑器_vue3 wangeditor

vue3 wangeditor

WangEditor的基本使用,图片上传功能
没详细讲解,只是做个记录。

Componets 下定义 WangEditor.vue 文件

<template>
    <div style="border: 1px solid #ccc;">
        <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" mode="default" />
        <Editor :style="{ 'height': height + 'px', 'overflow': 'hidden' }" v-model="newValue" :defaultConfig="editorConfig"
            mode="default" @onCreated="handleCreated" />
    </div>
</template>

<script setup>
import { defineProps, computed, shallowRef, defineEmits } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import Cookies from 'js-cookie'
import '@wangeditor/editor/dist/css/style.css' // 引入 css 

const props = defineProps({
    // 父组件 v-model 绑定的值
    modelValue: {
        type: String
    },
    // 富文本的高
    height: {
        type: [Number, String],
        default: 400
    }
})

const editorRef = shallowRef()

const emit = defineEmits(['update:modelValue'])

const toolbarConfig = {}

const editorConfig = { placeholder: '请输入内容...', MENU_CONF: {} }

editorConfig.MENU_CONF['uploadImage'] = {
    // 上传图片
    fieldName: 'file',
    // headers头部 需要添加不需要删掉
    headers: {
        Authorization: Cookies.get('Token'),
        ContentType: 'application/json;charset=utf-8'
    },
    // 图片上传的路径
    server: import.meta.env.VITE_APP_BASE_API + '/image/upload',
    // 图片上传返回的数据
    customInsert(res, insertFn) {
        let src = import.meta.env.VITE_APP_BASE_API + res.data.url // 有的会返回带域名的路径也有不带的根据自己的实际情况
        insertFn(src)
    },
}

editorConfig.MENU_CONF['uploadVideo'] = {

}

const newValue = computed({
    get() {
        return props.modelValue
    },
    set(value) {
        emit('update:modelValue', value)
    }
})

const handleCreated = (editor) => {
    editorRef.value = editor // 记录 editor 实例,重要!
    // console.log(editor.getAllMenuKeys()) // 这里输出可以看到 富文本中所有的功能
}

</script>

<style lang="scss" scoped></style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/635032
推荐阅读
相关标签
  

闽ICP备14008679号