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

Vue3使用富文本框(wangeditor)的方法總結(jié)

 更新時間:2024年01月18日 09:45:03   作者:mfxcyh  
項目中用到了富文本,選來選去選擇了wangeditor,下面這篇文章主要給大家介紹了關(guān)于Vue3使用富文本框(wangeditor)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

畢業(yè)涉及中使用到了富文本框,所以學(xué)習(xí)使用了wangeditor富文本框,現(xiàn)進行總結(jié)

1.安裝

npm install @wangeditor/editor --save
npm install @wangeditor/editor-for-vue@next --save

2.配置wangeditor組件(src/components/wangeditor.vue)

<template>
  <div style="border: 1px solid #ccc">
    <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
    />
    <Editor
        style="min-height: 250px; overflow-y: hidden;"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
    />
  </div>
</template>
//script標(biāo)簽中引入
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
export default {
  components: { Editor, Toolbar },
  setup(props,{emit}) {
    emits: ['select']
    // 編輯器實例,必須用 shallowRef
    const editorRef = shallowRef()

    // 內(nèi)容 HTML
    const valueHtml = ref('')

    //配置功能欄
    let toolbarConfig = {
      toolbarKeys: [
        'headerSelect',
        'blockquote',
        '|',
        'bold',
        'underline',
        'italic',
        {
          key: 'group-more-style',
          title: '更多',
          iconSvg:
              '<svg viewBox="0 0 1024 1024"><path d="M204.8 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path><path d="M505.6 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path><path d="M806.4 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path></svg>',
          menuKeys: ['through', 'code', 'sup', 'sub']
        },
        'color',
        'bgColor',
        '|',
        'fontSize',
        {
          key: 'group-justify',
          title: '對齊',
          iconSvg:
              '<svg viewBox="0 0 1024 1024"><path d="M768 793.6v102.4H51.2v-102.4h716.8z m204.8-230.4v102.4H51.2v-102.4h921.6z m-204.8-230.4v102.4H51.2v-102.4h716.8zM972.8 102.4v102.4H51.2V102.4h921.6z"></path></svg>',
          menuKeys: ['justifyLeft', 'justifyRight', 'justifyCenter', 'justifyJustify']
        },
        'todo',
        'fontFamily',
        {
          key: 'group-indent',
          title: '縮進',
          iconSvg:
              '<svg viewBox="0 0 1024 1024"><path d="M0 64h1024v128H0z m384 192h640v128H384z m0 192h640v128H384z m0 192h640v128H384zM0 832h1024v128H0z m0-128V320l256 192z"></path></svg>',
          menuKeys: ['indent', 'delIndent']
        },
        '|',
        'emotion',
        'insertLink',
        'uploadImage',
        'insertTable',
        'codeBlock',
        'divider',
        'clearStyle',
        '|',
        'undo',
        'redo',
      ]
    }

    
    const uploadImageList = ref([])
    const saveImageList = ref([])

    //上傳本地圖片
    function update(file,insertFn) {
      let formData = new FormData()
      formData.append('file', file)
      axios.post('http://localhost:8080/api/file/upload',formData,{
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        if (res.data.code == 0){
          const src = 'http://121.37.0.16:9000/public/'+ res.data.data.fileName[0]
          insertFn(src, '百度 logo', src)
        }
      })
    }

    function getOnInsertedImage(imageNode){
      uploadImageList.value.push(imageNode)
    }

    //編輯器配置
    let editorConfig =  {
      placeholder: '請輸入內(nèi)容...',
          // 所有的菜單配置,都要在 MENU_CONF 屬性下
          MENU_CONF: {
        insertImage:{
          onInsertedImage: getOnInsertedImage()
        },
        // 配置上傳圖片
        uploadImage: {
          customUpload: update
        }
      }
    }


    // 組件銷毀時,也及時銷毀編輯器
    onBeforeUnmount(() => {
      const editor = editorRef.value
      if (editor == null) return
      editor.destroy()
    })

    
    function copyObject(obj){
      return JSON.parse(JSON.stringify(obj));
    }
    const handleCreated = (editor) => {
      editorRef.value = editor // 記錄 editor 實例,重要!
      saveImageList.value = editor.getElemsByType('image')
      uploadImageList.value = copyObject(saveImageList.value)
      console.log('created', editor)
    }

    watch(() => valueHtml.value,()=>{
      //當(dāng)編輯器的內(nèi)容發(fā)生變化時,把值傳給父組件
        emit('select', valueHtml.value)
    })


    const handleChange = (editor) => { console.log('change:', editor.children) }
    const handleDestroyed = (editor) => { console.log('destroyed', editor) }
    const handleFocus = (editor) => { console.log('focus', editor) }
    const handleBlur = (editor) => { console.log('blur', editor) }
    const customAlert = (info, type) => { alert(`【自定義提示】${type} - ${info}`) }
    const customPaste = (editor, event, callback) => {
      console.log('ClipboardEvent 粘貼事件對象', event)
      // const html = event.clipboardData.getData('text/html') // 獲取粘貼的 html
      // const text = event.clipboardData.getData('text/plain') // 獲取粘貼的純文本
      // const rtf = event.clipboardData.getData('text/rtf') // 獲取 rtf 數(shù)據(jù)(如從 word wsp 復(fù)制粘貼)

      // 自定義插入內(nèi)容
      editor.insertText('xxx')

      // 返回 false ,阻止默認(rèn)粘貼行為
      event.preventDefault()
      callback(false) // 返回值(注意,vue 事件的返回值,不能用 return)

      // 返回 true ,繼續(xù)默認(rèn)的粘貼行為
      // callback(true)
    }

    //父組件調(diào)用子組件的方法清空編輯器內(nèi)容
    const abc =function (){
      valueHtml.value = ''
    }
    //暴露該方法,defineExpose要引入
    defineExpose({
      abc
    })

    return {
      editorRef,
      valueHtml,
      mode: 'default', // 或 'simple'
      toolbarConfig,
      editorConfig,
      handleCreated,
      handleChange,
      handleDestroyed,
      handleFocus,
      handleBlur,
      customAlert,
      customPaste,
      abc,
    };
  }
}

3.父組件中

//引入
import WangEditor from '../../../components/WangEditor.vue'

//注冊
components: {
    WangEditor
},
<a-form-model-item :wrapper-col="{ offset: 2, span: 24 }" name="introduction">
    <div>課程介紹:</div><br>
    <WangEditor
        class="WangEditor"
        @select="getRich"
        ref="childrenRef"
    />
</a-form-model-item>
 //當(dāng)編輯器的內(nèi)容更新時,獲取該值
const getRich = function (value){
    state.introduction = value
    console.log(value)
}

//獲取dom元素
const childrenRef = ref(null)

······
    //調(diào)用子組件的方法清空編輯器內(nèi)容
    childrenRef.value.abc()

總結(jié) 

到此這篇關(guān)于Vue3使用富文本框(wangeditor)的文章就介紹到這了,更多相關(guān)Vue3使用富文本框wangeditor內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在?Vue?中使用?iframe?嵌套頁面的步驟

    在?Vue?中使用?iframe?嵌套頁面的步驟

    這篇文章主要介紹了在?Vue?中使用?iframe?嵌套頁面,使用?iframe?技術(shù)可以實現(xiàn)多個頁面之間的數(shù)據(jù)傳遞和交互,提高了網(wǎng)站的整體性能和用戶體驗,需要的朋友可以參考下
    2023-05-05
  • Vue實現(xiàn)下載文件而非瀏覽器直接打開的方法

    Vue實現(xiàn)下載文件而非瀏覽器直接打開的方法

    對于瀏覽器來說,文本、圖片等可以直接打開的文件,不會進行自動下載,下面這篇文章主要給大家介紹了關(guān)于Vue實現(xiàn)下載文件而非瀏覽器直接打開的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • vue3+vite 動態(tài)引用靜態(tài)資源及動態(tài)引入assets文件夾圖片的多種方式

    vue3+vite 動態(tài)引用靜態(tài)資源及動態(tài)引入assets文件夾圖片的多種方式

    通過require動態(tài)引入, 發(fā)現(xiàn)報錯:require is not defind,這是因為 require 是屬于 Webpack 的方法,本文給大家介紹vue3+vite 動態(tài)引用靜態(tài)資源及動態(tài)引入assets文件夾圖片的多種方式,感興趣的朋友一起看看吧
    2023-10-10
  • Vue select 綁定動態(tài)變量的實例講解

    Vue select 綁定動態(tài)變量的實例講解

    這篇文章主要介紹了Vue select 綁定動態(tài)變量的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • vue具名插槽的基本使用實例

    vue具名插槽的基本使用實例

    Vue 中的插槽在開發(fā)組件的過程中其實是非常重要并且好用的。下面這篇文章主要給大家介紹了關(guān)于vue具名插槽基本使用的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • 詳解如何理解vue的key屬性

    詳解如何理解vue的key屬性

    這篇文章主要介紹了詳解如何理解vue的key屬性,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 解決Vite無法分析出動態(tài)import的類型,控制臺出警告的問題

    解決Vite無法分析出動態(tài)import的類型,控制臺出警告的問題

    這篇文章主要介紹了解決Vite無法分析出動態(tài)import的類型,控制臺出警告的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue動態(tài)菜單、動態(tài)路由加載以及刷新踩坑實戰(zhàn)

    vue動態(tài)菜單、動態(tài)路由加載以及刷新踩坑實戰(zhàn)

    這篇文章主要給大家介紹了關(guān)于vue動態(tài)菜單、動態(tài)路由加載以及刷新踩坑的相關(guān)資料,踩的這些坑其實是挺常見的,大家可以看看,避免遇到的時候再踩到同樣的坑,需要的朋友可以參考下
    2021-10-10
  • Vue3實現(xiàn)LuckSheet在線預(yù)覽Excel表格

    Vue3實現(xiàn)LuckSheet在線預(yù)覽Excel表格

    在前端開發(fā)中預(yù)覽Excel文件是常見的需求之一,本文將介紹如何使用Vue.js框架以及兩個優(yōu)秀的Excel庫——LuckyExcel和Luckysheet,來實現(xiàn)Excel文件在線預(yù)覽功能,希望對大家有所幫助
    2023-11-11
  • vue實現(xiàn)動態(tài)給data函數(shù)中的屬性賦值

    vue實現(xiàn)動態(tài)給data函數(shù)中的屬性賦值

    這篇文章主要介紹了vue實現(xiàn)動態(tài)給data函數(shù)中的屬性賦值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評論