欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3使用wangeditor封裝和自定義上傳文件官方教程

 更新時(shí)間:2023年06月25日 17:35:16   作者:張旭超  
這篇文章主要為大家介紹了vue3使用wangeditor封裝和自定義上傳文件的官方教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>

1、基本配置

官方教程 https://www.wangeditor.com/

2、封裝組件

/**
  initValue: 父組件傳遞過來的富文本框初始值,這里會實(shí)時(shí)監(jiān)聽,更新初始值,放置在彈窗中使用,沒有鉤子函數(shù)的更新。
  getEditorContent() 方法,父組件通過這個(gè)方法獲取富文本編輯器的內(nèi)容,包括數(shù)組格式的和html格式的內(nèi)容
*/
<template>
  <div v-html="valueHtml"></div>
  <div style="border: 1px solid #ccc">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editorRef"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      style="min-height: 100px; overflow-y: hidden; text-align: left;"
      v-model="valueHtml"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="handleCreated"
      @onChange="handleChange"
    />
  </div>
</template>
<script setup>
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { onBeforeUnmount, ref, shallowRef, onMounted, nextTick, watch } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import axios from 'axios'
// 初始值
const props = defineProps({
  initValue: String
})
const emits = defineEmits(['getEditorContent'])
// const emits = defineEmits([''])
let mode = ref('default')
// 編輯器實(shí)例,必須用 shallowRef
const editorRef = shallowRef()
// 內(nèi)容 HTML
const valueHtml = ref('')
// 模擬 ajax 異步獲取內(nèi)容
onMounted(() => {
  nextTick(() => { // 界面節(jié)點(diǎn)更新完以后再修改值。
    valueHtml.value = props.initValue
  })
})
// 工具欄配置
const toolbarConfig = {
  toolbarKeys: [
    // 菜單 key
    'headerSelect',
    'bold', // 加粗
    'italic', // 斜體
    'through', // 刪除線
    'underline', // 下劃線
    'bulletedList', // 無序列表
    'numberedList', // 有序列表
    'color', // 文字顏色
    'insertLink', // 插入鏈接
    'fontSize', // 字體大小
    'lineHeight', // 行高
    'uploadImage', // 上傳圖片
    'delIndent', // 縮進(jìn)
    'indent', // 增進(jìn)
    'deleteImage',//刪除圖片
    'divider', // 分割線
    'insertTable', // 插入表格
    'justifyCenter', // 居中對齊
    'justifyJustify', // 兩端對齊
    'justifyLeft', // 左對齊
    'justifyRight', // 右對齊
    'undo', // 撤銷
    'redo', // 重做
    'clearStyle', // 清除格式
    'fullScreen' // 全屏
  ]
}
const editorConfig = {
  placeholder: '請輸入內(nèi)容...', // 配置默認(rèn)提示
  // readOnly: true,
  MENU_CONF: {                // 配置上傳服務(wù)器地址
    uploadImage: {
      // 小于該值就插入 base64 格式(而不上傳),默認(rèn)為 0
      base64LimitSize: 5 * 1024, // 5kb
      // 單個(gè)文件的最大體積限制,默認(rèn)為 2M
      // maxFileSize: 1 * 1024 * 1024, // 1M
      // // 最多可上傳幾個(gè)文件,默認(rèn)為 100
      // maxNumberOfFiles: 5,
      // 選擇文件時(shí)的類型限制,默認(rèn)為 ['image/*'] 。如不想限制,則設(shè)置為 []
      allowedFileTypes: ['image/*'],
      // 自定義上傳
      async customUpload(file, insertFn) { // 文件上傳
        const formData = new FormData();
        formData.set('file', file)
        // 這里根據(jù)自己項(xiàng)目封裝情況,設(shè)置上傳地址
        axios.defaults.headers.common['Authorization'] = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjVhYzc3MDQxLTE3NGItNDEwZC1hOTJlLTVkYzU0YmRhMjllNiIsInVzZXJuYW1lIjoiYWRtaW4ifQ.GhdthjLmw4NP2WV2owYQS70tacRM-pX7NqI7Mo0_j_hatiqtSrqAr0RJC40osRETiQo_dacZcvNqATLsJHL77A'
        let result = await axios.post('https://zxc.test.cn/file/upload', formData)
         // 插入到富文本編輯器中,主意這里的三個(gè)參數(shù)都是必填的,要不然控制臺報(bào)錯(cuò):typeError: Cannot read properties of undefined (reading 'replace')
        insertFn(result.data.data.url, result.data.data.name, result.data.data.name)
      }
    }
  }
}
// 組件銷毀時(shí),也及時(shí)銷毀編輯器
onBeforeUnmount(() => {
    const editor = editorRef.value
    if (editor == null) return
    editor.destroy()
})
const handleCreated = (editor) => {
  editorRef.value = editor // 創(chuàng)建富文本編輯器
}
const handleChange = (info) => {
  // info.children 數(shù)組包含了數(shù)據(jù)類型和內(nèi)容,valueHtml.value內(nèi)容的html格式
  emits('getEditorContent', info.children, valueHtml.value)
}
watch(()=>props.initValue, (value) => {  // 父組件獲取初始值
  valueHtml.value = value
})
</script>

3、使用

父組件
<wang-editor :initValue="form.baseInfo.descInfo" @getEditorContent="onEditorChange"></wang-editor>
import WangEditor from '@/components/WangEditor'
const onEditorChange = (arr, html) => {
    console.log(arr, html)
}

以上就是vue3使用wangeditor封裝和自定義上傳文件官方教程的詳細(xì)內(nèi)容,更多關(guān)于vue3 wangeditor封裝上傳文件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論