1、安裝使用
安裝
yarn add @wangeditor/editor
# 或者 npm install @wangeditor/editor --save
yarn add @wangeditor/editor-for-vue
# 或者 npm install @wangeditor/editor-for-vue --save
在main.js中引入樣式
import '@wangeditor/editor/dist/css/style.css'
在使用編輯器的頁面引入js
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
components: { Editor, Toolbar },
模板
<template>
<div>
<div style="border: 1px solid #ccc; margin-top: 10px">
<!-- 工具欄 -->
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editor"
:defaultConfig="toolbarConfig"
/>
<!-- 編輯器 -->
<Editor
style="height: 400px; overflow-y: hidden"
:defaultConfig="editorConfig"
v-model="html"
@onChange="onChange"
@onCreated="onCreated"
/>
</div>
</div>
</template>
js
<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
export default {
name: "MyEditor",
components: { Editor, Toolbar },
data() {
return {
editor: null,
html: "<p>hello world</p>",
toolbarConfig: {},
editorConfig: {
placeholder: "請輸入內(nèi)容...",
// 所有的菜單配置,都要在 MENU_CONF 屬性下
MENU_CONF: {},
},
};
},
methods: {
onCreated(editor) {
this.editor = Object.seal(editor); // 【注意】一定要用 Object.seal() 否則會報錯
},
onChange(editor) {
console.log("onChange", editor.getHtml()); // onChange 時獲取編輯器最新內(nèi)容
},
},
mounted() {},
beforeDestroy() {
const editor = this.editor;
if (editor == null) return;
editor.destroy(); // 組件銷毀時,及時銷毀 editor ,重要!?。? },
};
</script>
?到這一步編輯器就可以正常顯示了
2、上傳圖片、視頻
上傳到后臺接口的可直接按照文檔這個配置就行接口返回格式也要可文檔上一致
?
?2)自定義上傳(一般上傳到別的服務(wù)器上,我這邊是上傳到七牛云服務(wù)器上)
在data配置上傳圖片、視頻文章來源:http://www.zghlxwxcb.cn/news/detail-675988.html
editorConfig: {
placeholder: "請輸入內(nèi)容...",
// 所有的菜單配置,都要在 MENU_CONF 屬性下
MENU_CONF: {
uploadImage: {
customUpload: async (file, insertFn) => {
let resultUrl = await this.upqiniu(file, file.name);
insertFn(resultUrl);
},
},
uploadVideo: {
customUpload: async (file, insertFn) => {
let resultUrl = await this.upqiniu(file, file.name);
insertFn(resultUrl);
},
},
},
},
?this.upqiniu是我寫的上傳服務(wù)器的代碼,最終將接口返回的地址return出去就行文章來源地址http://www.zghlxwxcb.cn/news/detail-675988.html
upqiniu(file, name) {
return new Promise((resolve) => {
let config = {
useCdnDomain: true, //表示是否使用 cdn 加速域名,為布爾值,true 表示使用,默認為 false。
region: null, // 根據(jù)具體提示修改上傳地區(qū),當(dāng)為 null 或 undefined 時,自動分析上傳域名區(qū)域
};
let putExtra = {
fname: `upload_pic_${name}`, //文件原文件名
params: {}, //用來放置自定義變量
mimeType: null, //用來限制上傳文件類型,為 null 時表示不對文件類型限制;限制類型放到數(shù)組里: ["image/png", "image/jpeg", "image/gif"]
};
var observable = qiniu.upload(
file,
`upload_pic_${name}`,
this.token,
putExtra,
config
);
observable.subscribe({
next: (result) => {
// 主要用來展示進度
},
error: (errResult) => {
// 失敗報錯信息
},
complete: (result) => {
// 接收成功后返回的信息
let url = "http://image.gewu.pro/" + result.key;
resolve(url);
},
});
});
},
到了這里,關(guān)于vue2+wangEditor5富文本編輯器(圖片視頻自定義上傳七牛云/服務(wù)器)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!