輕量級富文本編輯器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 拿到該錯誤碼進(jìn)行自定義處理
"errno": 0,
// data 是一個數(shù)組,返回若干圖片的線上地址
"data": [
"圖片1地址",
"圖片2地址",
"……"
]
}
這些基本就夠用了。官網(wǎng)也寫得很詳細(xì)滴
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 格式字符串!??!否則會報錯)
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 格式字符串!?。》駝t報錯
}
}
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é)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3中實現(xiàn)拖拽和縮放自定義看板 vue-grid-layout的方法
這篇文章主要介紹了Vue3中實現(xiàn)拖拽和縮放自定義看板 vue-grid-layout的方法,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
Vue3項目打包后部署到服務(wù)器 請求不到后臺接口解決方法
在本篇文章里小編給大家整理了關(guān)于Vue3項目打包后部署到服務(wù)器 請求不到后臺接口解決方法,有需要的朋友們可以參考下。2020-02-02
cesium開發(fā)之如何在vue項目中使用cesium,使用離線地圖資源
這篇文章主要介紹了cesium開發(fā)之如何在vue項目中使用cesium,使用離線地圖資源問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
vue使用高德地圖根據(jù)坐標(biāo)定位點(diǎn)的實現(xiàn)代碼
這篇文章主要介紹了vue使用高德地圖根據(jù)坐標(biāo)定位點(diǎn)的實現(xiàn)代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
Vue突然報錯doesn‘t?work?properly?without?JavaScript?enabled
最近在做項目的時候遇到了些問題,所以這篇文章主要給大家介紹了關(guān)于Vue突然報錯doesn‘t?work?properly?without?JavaScript?enabled的解決方法,需要的朋友可以參考下2023-01-01

