富文本的引入使用
新富文本使用
1.npm install wangeditor
2.在main.js 全域引入 富文本組件
//全域引入新富文本
import editorBar from "@/components/wangEnduit";
Vue.component('editorBar', editorBar)
下圖為示例

3.頁面參考
<editor-bar v-model="EidtAccountForm.content" :isClear="isClear" @change="change"></editor-bar>
下圖是例子:
編輯的例子:

添加的例子:

4.系結函式的定義
下圖是例子
// 獲取新富文本值
change(val) {
console.log("val", val);
//this.tgmsg = val; //這里將獲取到的值 賦值給 對應得變數
},

這是封裝好的富文本參考檔案
<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";
export default {
name: "editoritem",
data() {
return {
// fileUploadAdd: this.apiList.upload,
editor: null,
info_: null,
};
},
model: {
prop: "value",
event: "change",
},
props: {
value: {
type: String,
default: "",
},
isClear: {
type: Boolean,
default: false,
},
//用于修改的時候資料回顯
// contxt: {
// type: String,
// default: "",
// },
},
watch: {
isClear(val) {
// 觸發清除文本域內容
if (val) {
this.editor.txt.clear();
this.info_ = null;
}
},
value: function (value) {
// 使用v-model時,設定初始值
if (value !== this.editor.txt.html()) {
this.editor.txt.html(this.value);
}
},
//value為編輯框輸入的內容,這里我監聽了一下值,當父組件呼叫得時候,如果給value賦值了,子組件將會顯示父組件賦給的值
},
mounted() {
this.seteditor();
// 這一步非常重要,用于富文本修改資訊的時候,資料回顯
// this.contxt是父子傳參,動態傳值的
// this.editor.txt.html(this.contxt);
this.editor.txt.html(this.value);
},
methods: {
seteditor() {
this.editor = new E(this.$refs.toolbar, this.$refs.editor);
// 通過 url 引數配置 debug 模式。url 中帶有 wangeditor_debug_mode=1 才會開啟 debug 模式
this.editor.customConfig.debug =
location.href.indexOf("wangeditor_debug_mode=1") > 0;
this.editor.customConfig.uploadImgShowBase64 = false; // base64存盤圖片
this.editor.customConfig.showLinkImg = false; // 隱藏網路圖片
this.editor.customConfig.uploadImgServer =
this.api.BASE_URL + this.api.BASE_URL_ADMIN_PATH + this.apiList.upload; // 配置服務器端地址
this.editor.customConfig.uploadImgHeaders = {
token: localStorage.getItem(this.api.AUTH_TOKEN_FLAG), // 設定請求頭
}; // 自定義 header
this.editor.customConfig.uploadFileName = "file"; // 后端接受上傳檔案的引數名
this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024; // 將圖片大小限制為 2M
this.editor.customConfig.uploadImgMaxLength = 6; // 限制一次最多上傳 3 張圖片
this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000; // 設定超時時間
// 自定義引數
this.editor.customConfig.uploadImgParams = {
item: "tag",
time: this.common.fromData.time,
platform: this.common.fromData.platform,
version: this.common.fromData.version,
};
// 配置選單
this.editor.customConfig.menus = [
"head", // 標題
"bold", // 粗體
"fontSize", // 字號
"fontName", // 字體
"italic", // 斜體
"underline", // 下劃線
"strikeThrough", // 洗掉線
"foreColor", // 文字顏色
"backColor", // 背景顏色
"link", // 插入鏈接
"list", // 串列
"justify", // 對齊方式
"quote", // 參考
"emoticon", // 表情
"image", // 插入圖片
"table", // 表格
// "video", // 插入視頻
"code", // 插入代碼
"undo", // 撤銷
"redo", // 重復
"fullscreen", // 全屏
];
this.editor.customConfig.uploadImgHooks = {
fail: (xhr, editor, result) => {
// 插入圖片失敗回呼
console.log("fail", xhr, editor, result);
},
success: (xhr, editor, result) => {
// 圖片上傳成功回呼
console.log("success", xhr, editor, result);
},
timeout: (xhr, editor) => {
// 網路超時的回呼
},
error: (xhr, editor) => {
// 圖片上傳錯誤的回呼
console.log("error", xhr, editor);
},
customInsert: (insertImg, result, editor) => {
// 圖片上傳成功,插入圖片的回呼
// console.log("r", result);
let url = result.data[0].url;
insertImg(url); // insertImg()為插入圖片的函式
},
};
this.editor.customConfig.onchange = (html) => {
this.info_ = html; // 系結當前逐漸地值
this.$emit("change", this.info_); // 將內容同步到父組件中
};
// 創建富文本編輯器
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;
min-height: 300px;
}
</style>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/127430.html
標籤:JavaScript
上一篇:THML+CSS
下一篇:視頻播放器
