1. wangEditor4介紹
wangEditor4 —— 輕量級(jí) web 富文本編輯器静暂,配置方便搏熄,使用簡(jiǎn)單睡毒。支持 IE10+ 瀏覽器来惧。
2. 下載
注意: wangeditor都是小寫(xiě)字母
npm install wangeditor
3. 定義編輯器子組件
-
創(chuàng)建富文本編輯器子組件 editor.vue
template
<template> <div class="editor" id="box"></div> </template>
script
import E from "wangeditor"; export default { data() { return {}; }, mounted() { this.initE(); }, methods: { initE() { this.editor = new E("#box"); //這里各位小伙伴可以查詢官網(wǎng),根據(jù)自己的需求進(jìn)行菜單欄調(diào)整 this.editor.config.menus = [ "head", // 標(biāo)題 "bold", // 粗體 "fontSize", // 字號(hào) "fontName", // 字體 "italic", // 斜體 "underline", // 下劃線 "strikeThrough", // 刪除線 "foreColor", // 文字顏色 "backColor", // 背景顏色 "link", // 插入鏈接 "list", // 列表 "justify", // 對(duì)齊方式 "quote", // 引用 "emoticon", // 表情 "image", // 插入圖片 "table", // 表格 "code", // 插入代碼 "undo", // 撤銷 "redo", // 重復(fù) ]; //為true演顾,則上傳后的圖片轉(zhuǎn)為base64編碼供搀,為false,則上傳圖片到服務(wù)器,二者不能同時(shí)使用 this.editor.config.uploadImgShowBase64 = false; //服務(wù)器接收的上傳圖片的屬性名 this.editor.config.uploadFileName = "file"; //服務(wù)器上傳圖片的接口地址 this.editor.config.uploadImgServer = `http://localhost:3000/upload`; // 默認(rèn)限制圖片大小是 5M 钠至,可以自己修改葛虐。 this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024; // 2M // 限制類型,可自定義配置,默認(rèn)為: ['jpg', 'jpeg', 'png', 'gif', 'bmp'] this.editor.config.uploadImgAccept = ['jpg', 'jpeg',"png", "gif", "bmp", "webp"]; //上傳圖片過(guò)程中鉤子函數(shù) this.editor.config.uploadImgHooks = { // 上傳圖片之前 before: function (xhr) { // console.log(xhr); // // 可阻止圖片上傳 // return { // prevent: true, // msg: "需要提示給用戶的錯(cuò)誤信息", // }; }, // 圖片上傳并返回了結(jié)果,圖片插入已成功 success: function (xhr) { console.log("success", xhr); }, // 圖片上傳并返回了結(jié)果棉钧,但圖片插入時(shí)出錯(cuò)了 fail: function (xhr, editor, resData) { console.log("fail", resData); }, // 上傳圖片出錯(cuò)屿脐,一般為 http 請(qǐng)求的錯(cuò)誤 error: function (xhr, editor, resData) { console.log("error", xhr, resData); }, // 上傳圖片超時(shí) timeout: function (xhr) { console.log("timeout"); }, }; // 在輸入內(nèi)容時(shí),把內(nèi)容傳給父組件 this.editor.config.onchange = (html) => { this.$emit("change", html); // 將內(nèi)容同步到父組件中 }; this.editor.create(); //初始化富文本編輯器時(shí)顯示的內(nèi)容 this.editor.txt.html("<h1>haha</h1>"); }, }, };
4. 圖片上傳注意事項(xiàng)
4.1 響應(yīng)數(shù)據(jù)格式要求
只有響應(yīng)數(shù)據(jù)的格式符合下列要求宪卿,才會(huì)調(diào)用success和fail等回調(diào)函數(shù)
- 響應(yīng)數(shù)據(jù)格式
{
// errno 即錯(cuò)誤代碼的诵,0 表示沒(méi)有錯(cuò)誤。
// 如果有錯(cuò)誤佑钾,errno != 0西疤,可通過(guò)下文中的監(jiān)聽(tīng)函數(shù) fail 拿到該錯(cuò)誤碼進(jìn)行自定義處理
"errno": 0,
// data 是一個(gè)數(shù)組,返回圖片Object休溶,Object中包含需要包含url代赁、alt和href三個(gè)屬性,它們分別代表圖片地址撒遣、圖片文字說(shuō)明和跳轉(zhuǎn)鏈接,alt和href屬性是可選的,可以不設(shè)置或設(shè)置為空字符串,需要注意的是url是一定要填的管跺。
"data": [
{
url: "圖片地址",
alt: "圖片文字說(shuō)明",
href: "跳轉(zhuǎn)鏈接"
},
{
url: "圖片地址1",
alt: "圖片文字說(shuō)明1",
href: "跳轉(zhuǎn)鏈接1"
},
"……"
]
}
- 回調(diào)函數(shù)
//上傳圖片過(guò)程中鉤子函數(shù)
this.editor.config.uploadImgHooks = {
// 上傳圖片之前
before: function (xhr) {
// console.log(xhr);
// // 可阻止圖片上傳
// return {
// prevent: true,
// msg: "需要提示給用戶的錯(cuò)誤信息",
// };
},
// 圖片上傳并返回了結(jié)果,圖片插入已成功
success: function (xhr) {
console.log("success", xhr);
},
// 圖片上傳并返回了結(jié)果禾进,但圖片插入時(shí)出錯(cuò)了
fail: function (xhr, editor, resData) {
console.log("fail", resData);
},
// 上傳圖片出錯(cuò)豁跑,一般為 http 請(qǐng)求的錯(cuò)誤
error: function (xhr, editor, resData) {
console.log("error", xhr, resData);
},
// 上傳圖片超時(shí)
timeout: function (xhr) {
console.log("timeout");
},
};
-
express后端返回示例
//圖片上傳 router.post("/upload", upload.single("file"), (req, res) => { // 需要返回圖片的訪問(wèn)地址 域名+上傳文件夾+文件名 const url = "http://localhost:3000/upload/" + req.file.filename res.json({ errno: 0, "data": [ { url, alt: '可愛(ài)', href:"http://www.baidu.com" } ] }) })
4.2 響應(yīng)數(shù)據(jù)不符合格式要求
-
express后端返回示例
//圖片上傳 router.post("/upload", upload.single("file"), (req, res) => { // 需要返回圖片的訪問(wèn)地址 域名+上傳文件夾+文件名 const url = "http://localhost:3000/upload/" + req.file.filename res.json({url}) })
自定義回調(diào)函數(shù),插入圖片
customInsert: function (insertImgFn, result) {
//result為上傳圖片成功的時(shí)候返回的數(shù)據(jù)泻云,這里我打印了一下發(fā)現(xiàn)后臺(tái)返回的是data:[{url:"路徑的形式"},...]
// console.log(result.data[0].url)
//insertImgFn()為插入圖片的函數(shù)
//循環(huán)插入圖片
// for (let i = 0; i < 1; i++) {
// console.log(result)
let url = "http://localhost:3000/upload/" + result.url;
insertImgFn(url);
}
5. 其它API
- editor.txt.html() 設(shè)置編輯器內(nèi)容艇拍。
-
editor.txt.append('<p>追加的內(nèi)容</p>')
繼續(xù)追加內(nèi)容。 -
editor.txt.html()
獲取 html -
editor.txt.clear()
清空編輯器內(nèi)容宠纯。
6. 父組件使用富文本編輯器子組件
<template>
<div id="app">
<editor @change="change"></editor>
<button @click="send()">提交</button>
</div>
</template>
<script>
import editor from "@/views/editor.vue";
export default {
components: { editor },
data(){
return {
result: ''
}
},
methods: {
//提交富文本編輯器的內(nèi)容
send(){
console.log('send',this.result);
},
change(html){
this.result = html
}
}
};
</script>
<style lang="scss">
</style>