关于在vue项目中使用wangEditor
生活随笔
收集整理的這篇文章主要介紹了
关于在vue项目中使用wangEditor
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1,vue中安裝wangEditor
?使用的npm安裝 npm install wangeditor --save
2,創(chuàng)建公用組件?
在components中創(chuàng)建wangEnduit文件夾
組件內(nèi)容為
<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 {// uploadPath, editor: null,info_: null}},model: {prop: 'value',event: 'change'},props: {value: {type: String,default: ''},isClear: {type: Boolean,default: false}},watch: {isClear(val) {// 觸發(fā)清除文本域內(nèi)容if (val) {this.editor.txt.clear()this.info_ = null}},value: function(value) {if (value !== this.editor.txt.html()) {this.editor.txt.html(this.value)}}//value為編輯框輸入的內(nèi)容,這里我監(jiān)聽(tīng)了一下值,當(dāng)父組件調(diào)用得時(shí)候,如果給value賦值了,子組件將會(huì)顯示父組件賦給的值 },mounted() {this.seteditor()this.editor.txt.html(this.value)},methods: {seteditor() {// http://192.168.2.125:8080/admin/storage/createthis.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 = { }// 自定義 headerthis.editor.customConfig.uploadFileName = 'file' // 后端接受上傳文件的參數(shù)名this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024 // 將圖片大小限制為 2Mthis.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.urlinsertImg(url)// } }}this.editor.customConfig.onchange = (html) => {this.info_ = html // 綁定當(dāng)前逐漸地值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;min-height: 500px;} </style>?
3 在父組件中調(diào)用
<template> <div> <editor-bar v-model="detail" :isClear="isClear" @change="change"></editor-bar> </div> </template>import EditorBar from './editoritem' components: { EditorBar }, data() {return {isClear: false,detail:""}}, methods: {change(val) {console.log(val)},}?
轉(zhuǎn)載于:https://www.cnblogs.com/huge1122/p/11346115.html
總結(jié)
以上是生活随笔為你收集整理的关于在vue项目中使用wangEditor的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: step4 . day7 进程间的通信方
- 下一篇: MVC项目实践,在三层架构下实现Spor