Vue中富文本編輯器wangEnduit的使用方式
在vue文件中寫(xiě)入
<template lang="html">
<div class="editor">
<div ref="toolbar" class="toolbar">
</div>
<div ref="editor" class="text">
</div>
</div>
</template>
<script>
import E from "wangeditor";
import { log } from "util";
export default {
name: "editoritem",
data() {
return {
// uploadPath,
editor: null,
info_: null
};
},
model: {
prop: "value",
event: "change"
},
props: {
value: {
type: String,
default: "<p>歡迎使用wangEditor編輯器</p>"
},
isClear: {
type: Boolean,
default: false
},
articleMain: {
type: String,
default: ""
},
editorHeight: {}
},
watch: {
isClear(val) {
// 觸發(fā)清除文本域內(nèi)容
if (val) {
this.editor.txt.clear();
this.info_ = null;
}
},
articleMain: {
handler: function(newValue, oldValue) {
this.articleMain = newValue;
this.editor.txt.html(this.articleMain);
}
},
//value為編輯框輸入的內(nèi)容,這里我監(jiān)聽(tīng)了一下值,當(dāng)父組件調(diào)用得時(shí)候,如果給value賦值了,子組件將會(huì)顯示父組件賦給的值
editorHeight: {
handler: function(newValue, oldValue) {
// doSomeThing();
document.getElementsByClassName(
"w-e-text-container"
)[0].style.height = newValue;
},
deep: true
}
},
mounted() {
this.seteditor();
this.changeEditDivHeight();
// this.editor.txt.html(this.value);
this.editor.txt.html(this.articleMain);
},
methods: {
changeEditDivHeight() {
document.getElementsByClassName(
"w-e-text-container"
)[0].style.height = this.editorHeight;
document.getElementsByClassName(" w-e-text")[0].style.minHeight = "100px";
},
seteditor() {
// http://192.168.2.125:8080/admin/storage/create
this.editor = new E(this.$refs.toolbar, this.$refs.editor);
this.editor.customConfig.uploadImgShowBase64 = false; // base 64 存儲(chǔ)圖片
this.editor.customConfig.uploadImgServer =
"http://otp.cdinfotech.top/file/upload_images"; // 配置服務(wù)器端地址
this.editor.customConfig.uploadImgHeaders = {}; // 自定義 header
this.editor.customConfig.uploadFileName = "file"; // 后端接受上傳文件的參數(shù)名
this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024; // 將圖片大小限制為 2M
this.editor.customConfig.uploadImgMaxLength = 6; // 限制一次最多上傳 3 張圖片
this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000; // 設(shè)置超時(shí)時(shí)間
// 配置菜單
this.editor.customConfig.menus = [
"head", // 標(biāo)題
"bold", // 粗體
"fontSize", // 字號(hào)
// "fontName", // 字體
"italic", // 斜體
"underline", // 下劃線
"strikeThrough", // 刪除線
"foreColor", // 文字顏色
"backColor", // 背景顏色
"link", // 插入鏈接
"list", // 列表
"justify", // 對(duì)齊方式
"quote", // 引用
// "emoticon", // 表情
// 'image', // 插入圖片
"table", // 表格
// 'video', // 插入視頻
"code", // 插入代碼
"undo", // 撤銷
"redo", // 重復(fù)
"fullscreen" // 全屏
];
this.editor.customConfig.uploadImgHooks = {
fail: (xhr, editor, result) => {
// 插入圖片失敗回調(diào)
},
success: (xhr, editor, result) => {
// 圖片上傳成功回調(diào)
},
timeout: (xhr, editor) => {
// 網(wǎng)絡(luò)超時(shí)的回調(diào)
},
error: (xhr, editor) => {
// 圖片上傳錯(cuò)誤的回調(diào)
},
customInsert: (insertImg, result, editor) => {
// 圖片上傳成功,插入圖片的回調(diào)
//result為上傳圖片成功的時(shí)候返回的數(shù)據(jù),這里我打印了一下發(fā)現(xiàn)后臺(tái)返回的是data:[{url:"路徑的形式"},...]
// console.log(result.data[0].url)
//insertImg()為插入圖片的函數(shù)
//循環(huán)插入圖片
// for (let i = 0; i < 1; i++) {
// console.log(result)
let url = "http://otp.cdinfotech.top" + result.url;
insertImg(url);
// }
}
};
this.editor.customConfig.onchange = html => {
var test = this.editor.txt.text();
test = test.replace(/ /gi, "");
test = test.replace(/ /gi, "");
if (test === "") {
this.$emit("change", "");
} else {
this.info_ = html;
this.$emit("change", this.info_); // 將內(nèi)容同步到父組件中
}
};
// 創(chuàng)建富文本編輯器
this.editor.create();
}
}
};
</script>
<style lang="css">
.editor {
width: 100%;
margin: 0 auto;
position: relative;
z-index: 0;
}
.toolbar {
border: 1px solid #ccc;
}
.text {
border: 1px solid #ccc;
}
</style>
在使用的地方作為組件注冊(cè)導(dǎo)入

image.png
寫(xiě)前面三個(gè)就可以了 下面兩個(gè)是我的使用方式

image.png
在methods寫(xiě)入change函數(shù)

image.png
this.editor.txt.text() 為文本內(nèi)容, this.editor.txt.html為帶標(biāo)簽的文本內(nèi)容,使用$emit方式進(jìn)行組件傳值

image.png