輕量級富文本編輯器wangEditor結(jié)合vue使用方法示例
1、安裝
使用npm下載: `npm install wangeditor`
2、 創(chuàng)建實例
(1)基本用法:
<template> <div> <div id="editor" class="editor"></div> </div> </template> <script> import E from 'wangeditor' export default { name: 'editor', mounted () { var editor = new E('#editor') editor.customConfig.onchange = (html) => { this.formArticle.content = html } editor.create() } } </script>
效果如圖
(2)自定義高度寫法如下:把菜單和編輯框分開
<template> <div> <div id="editorMenu" class="editorMenu"></div> <div id="editor" class="editor"></div> </div> </template>
<script> import E from 'wangeditor' export default { name: 'editor', mounted () { var editor = new E('#editorMenu', '#editor') editor.customConfig.onchange = (html) => { this.formArticle.content = html } editor.create() } } </script>
<style scoped> .editorMenu { border: 1px solid #ccc; } .editor { margin-top: -1px;//將多出來的一像素邊框隱藏 border: 1px solid #ccc; min-height: 400px;//編輯框最小高度 height:auto;//編輯框高度超過最小高度會根據(jù)內(nèi)容高度自適應(yīng) } </style>
3、上傳圖片
(1)editor.customConfig.uploadImgShowBase64 = true // 使用 base64 保存圖片
(2)editor.customConfig.uploadImgServer = '/upload' // 上傳服務(wù)器端地址
/upload是上傳圖片的服務(wù)器端接口,接口返回的數(shù)據(jù)格式如下(固定的,否則會取不到圖片地址?。。。?br />
{ // errno 即錯誤代碼,0 表示沒有錯誤。 // 如果有錯誤,errno != 0,可通過下文中的監(jiān)聽函數(shù) fail 拿到該錯誤碼進行自定義處理 "errno": 0, // data 是一個數(shù)組,返回若干圖片的線上地址 "data": [ "圖片1地址", "圖片2地址", "……" ] }
這些基本就夠用了。官網(wǎng)也寫得很詳細滴
wangeditor3+vue2.0簡單例子
把wangeditor作為組件的形式使用
子組件中
<template> <div id="wangeditor"> <div ref="editorElem" style="text-align:left"></div> </div> </template> <script> import E from 'wangeditor' export default { name: 'editorElem', data () { return { editorContent: '', } }, props:['catchData'], //接收父組件的方法 mounted() { var editor = new E(this.$refs.editorElem) //創(chuàng)建富文本實例 editor.customConfig.onchange = (html) => { this.editorContent = html this.catchData(html) //把這個html通過catchData的方法傳入父組件 } editor.customConfig.uploadImgServer = '你的上傳圖片的接口' editor.customConfig.uploadFileName = '你自定義的文件名' editor.customConfig.uploadImgHeaders = { 'Accept': '*/*', 'Authorization':'Bearer ' + token //頭部token } editor.customConfig.menus = [ //菜單配置 'head', 'list', // 列表 'justify', // 對齊方式 'bold', 'fontSize', // 字號 'italic', 'underline', 'image', // 插入圖片 'foreColor', // 文字顏色 'undo', // 撤銷 'redo', // 重復(fù) ] //下面是最重要的的方法 editor.customConfig.uploadImgHooks = { before: function (xhr, editor, files) { // 圖片上傳之前觸發(fā) // xhr 是 XMLHttpRequst 對象,editor 是編輯器對象,files 是選擇的圖片文件 // 如果返回的結(jié)果是 {prevent: true, msg: 'xxxx'} 則表示用戶放棄上傳 // return { // prevent: true, // msg: '放棄上傳' // } }, success: function (xhr, editor, result) { // 圖片上傳并返回結(jié)果,圖片插入成功之后觸發(fā) // xhr 是 XMLHttpRequst 對象,editor 是編輯器對象,result 是服務(wù)器端返回的結(jié)果 this.imgUrl=Object.values(result.data).toString() }, fail: function (xhr, editor, result) { // 圖片上傳并返回結(jié)果,但圖片插入錯誤時觸發(fā) // xhr 是 XMLHttpRequst 對象,editor 是編輯器對象,result 是服務(wù)器端返回的結(jié)果 }, error: function (xhr, editor) { // 圖片上傳出錯時觸發(fā) // xhr 是 XMLHttpRequst 對象,editor 是編輯器對象 }, timeout: function (xhr, editor) { // 圖片上傳超時時觸發(fā) // xhr 是 XMLHttpRequst 對象,editor 是編輯器對象 }, // 如果服務(wù)器端返回的不是 {errno:0, data: [...]} 這種格式,可使用該配置 // (但是,服務(wù)器端返回的必須是一個 JSON 格式字符串?。。》駝t會報錯) customInsert: function (insertImg, result, editor) { // 圖片上傳并返回結(jié)果,自定義插入圖片的事件(而不是編輯器自動插入圖片?。。。? // insertImg 是插入圖片的函數(shù),editor 是編輯器對象,result 是服務(wù)器端返回的結(jié)果 // 舉例:假如上傳圖片成功后,服務(wù)器端返回的是 {url:'....'} 這種格式,即可這樣插入圖片: let url = Object.values(result.data) //result.data就是服務(wù)器返回的圖片名字和鏈接 JSON.stringify(url) //在這里轉(zhuǎn)成JSON格式 insertImg(url) // result 必須是一個 JSON 格式字符串?。?!否則報錯 } } editor.create() },
父組件中
<template> <div id="father"> <wangeditor :catchData="catchData"></wangeditor> </div> </template> <script> import wangeditor from './wangeditor' data(){ return{ content:"" } }, methods:{ catchData(value){ this.content=value //在這里接受子組件傳過來的參數(shù),賦值給data里的參數(shù) } }, components: { wangeditor }, </script>
上面字最多的地方好好看清楚,只有做了customInsert: function (insertImg, result, editor){}里的步驟,圖片才會在富文本中顯示,否則是不會自動顯示。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
集成vue到j(luò)query/bootstrap項目的方法
下面小編就為大家分享一篇集成vue到j(luò)query/bootstrap項目的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02vue 數(shù)據(jù)雙向綁定的實現(xiàn)方法
這篇文章主要介紹了vue 數(shù)據(jù)雙向綁定的實現(xiàn)方法,幫助大家更好的理解和學(xué)習使用vue框架,感興趣的朋友可以了解下2021-03-03VSCode前端Vue項目引入Element-ui組件三步簡單操作方法
elementui相當于一個庫,封裝好的內(nèi)容,我們引入到vue項目中,就可用庫中的內(nèi)容,這篇文章主要給大家介紹了關(guān)于VSCode前端Vue項目引入Element-ui組件的三步簡單操作方法,需要的朋友可以參考下2024-07-07利用HBuilder打包前端開發(fā)webapp為apk的方法
下面小編就為大家?guī)硪黄肏Builder打包前端開發(fā)webapp為apk的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11解決VUE自定義拖拽指令時 onmouseup 與 click事件沖突問題
這篇文章主要介紹了解決VUE自定義拖拽指令時 onmouseup 與 click事件沖突問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Vue實例中生命周期created和mounted的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Vue實例中生命周期created和mounted區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面跟著小編來一起學(xué)習學(xué)習吧。2017-08-08$router.push()中通過path跳轉(zhuǎn)和通過name跳轉(zhuǎn)區(qū)別解析
今天在路由跳轉(zhuǎn)傳參時發(fā)現(xiàn)params傳參接收到的總是為空,才發(fā)現(xiàn)通過path和name傳參是有區(qū)別的,這篇文章主要介紹了$router.push()中通過path跳轉(zhuǎn)和通過name跳轉(zhuǎn)有什么區(qū)別,需要的朋友可以參考下2023-11-11