在vue中使用wangEditor富文本編輯器


1.安裝wangEditor
    npm install wangeditor
 
2.創建公共組件
在components中創建wangEnduit文件夾
 
3.編寫index.vue組件
  1. <template lang="html">  
  2.   <div class="editor">  
  3.     <div ref="toolbar" class="toolbar">  
  4.     </div>  
  5.     <div ref="editor" class="text">  
  6.     </div>  
  7.   </div>  
  8. </template>  
  9.   
  10. <script>  
  11.   import E from 'wangeditor'  
  12.   import 'wangeditor/release/wangEditor.min.css'  
  13.   export default {  
  14.     name: 'editoritem',  
  15.     data() {  
  16.       return {  
  17.         // uploadPath,  
  18.         editor: null,  
  19.         info_: null  
  20.       }  
  21.     },  
  22.     model: {  
  23.       prop: 'value',  
  24.       event: 'change'  
  25.     },  
  26.     props: {  
  27.       value: {  
  28.         type: String,  
  29.         default''  
  30.       },  
  31.       isClear: {  
  32.         type: Boolean,  
  33.         defaultfalse  
  34.       }  
  35.     },  
  36.     watch: {  
  37.       isClear(val) {  
  38.         // 觸發清除文本域內容  
  39.         if (val) {  
  40.           this.editor.txt.clear()  
  41.           this.info_ = null  
  42.         }  
  43.       },  
  44.       value: function(value) {  
  45.         if (value !== this.editor.txt.html()) {  
  46.           this.editor.txt.html(this.value)  
  47.         }  
  48.       }  
  49.       //value為編輯框輸入的內容,這里我監聽了一下值,當父組件調用得時候,如果給value賦值了,子組件將會顯示父組件賦給的值  
  50.     },  
  51.     mounted() {  
  52.       this.seteditor()  
  53.       this.editor.txt.html(this.value)  
  54.     },  
  55.     methods: {  
  56.       seteditor() {  
  57.         this.editor = new E(this.$refs.toolbar, this.$refs.editor)  
  58.         this.editor.customConfig.uploadImgShowBase64 = false // base 64 存儲圖片  
  59.         this.editor.customConfig.uploadImgServer = ''// 填寫配置服務器端地址  
  60.         this.editor.customConfig.uploadImgHeaders = { }// 自定義 header  
  61.         this.editor.customConfig.uploadFileName = 'file' // 后端接受上傳文件的參數名  
  62.         this.editor.customConfig.uploadImgParams = {  
  63.             // 如果版本 <=v3.1.0 ,屬性值會自動進行 encode ,此處無需 encode  
  64.             // 如果版本 >=v3.1.1 ,屬性值不會自動 encode ,如有需要自己手動 encode  
  65.             file_type: 'img'  
  66.         }  
  67.         this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024 // 將圖片大小限制為 2M  
  68.         this.editor.customConfig.uploadImgMaxLength = 6 // 限制一次最多上傳 6 張圖片  
  69.         this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000 // 設置超時時間  
  70.         // 自定義 onchange 觸發的延遲時間,默認為 200 ms  
  71.         // this.editor.customConfig.onchangeTimeout = 1000 // 單位 ms  
  72.         // 隱藏�網絡圖片�tab  
  73.         // this.editor.customConfig.showLinkImg = false  
  74.         // 表情面板可以有多個 tab ,因此要配置成一個數組。數組每個元素代表一個 tab 的配置  
  75.         // this.editor.customConfig.emotions = [  
  76.         //     {  
  77.         //         // tab 的標題  
  78.         //         title: '默認',  
  79.         //         // type -> 'emoji' / 'image'  
  80.         //         type: 'image',  
  81.         //         // content -> 數組  
  82.         //         content: [  
  83.         //             {  
  84.         //                 alt: '[壞笑]',  
  85.         //                 src: 'http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/50/pcmoren_huaixiao_org.png'  
  86.         //             },  
  87.         //             {  
  88.         //                 alt: '[舔屏]',  
  89.         //                 src: 'http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/40/pcmoren_tian_org.png'  
  90.         //             },  
  91.         //             {  
  92.         //                 alt: "[哈哈]",  
  93.         //                 src: "http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/8f/2018new_haha_org.png",  
  94.         //             },  
  95.         //             {  
  96.         //                 src : "http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/7a/shenshou_thumb.gif",  
  97.         //                 alt : "[草泥馬]"  
  98.         //             }  
  99.         //         ]  
  100.         //     },  
  101.         //     {  
  102.         //         // tab 的標題  
  103.         //         title: 'emoji',  
  104.         //         // type -> 'emoji' / 'image'  
  105.         //         type: 'emoji',  
  106.         //         // content -> 數組  
  107.         //         content: ['😀', '😃', '😄', '😁', '😆']  
  108.         //     }  
  109.         // ],  
  110.         // 配置菜單  
  111.         this.editor.customConfig.menus = [  
  112.           'head'// 標題  
  113.           'bold'// 粗體  
  114.           'fontSize'// 字號  
  115.           'fontName'// 字體  
  116.           'italic'// 斜體  
  117.           'underline'// 下划線  
  118.           'strikeThrough'// 刪除線  
  119.           'foreColor'// 文字顏色  
  120.           'backColor'// 背景顏色  
  121.           'link'// 插入鏈接  
  122.           'list'// 列表  
  123.           'justify'// 對齊方式  
  124.           'quote'// 引用  
  125.           'emoticon'// 表情  
  126.           'image'// 插入圖片  
  127.           'table'// 表格  
  128.           'video'// 插入視頻  
  129.           'code'// 插入代碼  
  130.           'undo'// 撤銷  
  131.           'redo'// 重復  
  132.           'fullscreen' // 全屏  
  133.         ]  
  134.   
  135.         this.editor.customConfig.uploadImgHooks = {  
  136.           fail: (xhr, editor, result) => {  
  137.             // 插入圖片失敗回調  
  138.           },  
  139.           success: (xhr, editor, result) => {  
  140.             // 圖片上傳成功回調  
  141.           },  
  142.           timeout: (xhr, editor) => {  
  143.             // 網絡超時的回調  
  144.           },  
  145.           error: (xhr, editor) => {  
  146.             // 圖片上傳錯誤的回調  
  147.           },  
  148.           customInsert: (insertImg, result, editor) => {  
  149.             // 圖片上傳成功,插入圖片的回調  
  150.             //result為上傳圖片成功的時候返回的數據,這里我打印了一下發現后台返回的是data:[{url:"路徑的形式"},...]  
  151.             // console.log(result.data[0].url)  
  152.             //insertImg()為插入圖片的函數  
  153.              //循環插入圖片  
  154.             // for (let i = 0; i < 1; i++) {  
  155.             if (result.code == 200) {  
  156.                 let url = result.data.image_url  
  157.                 JSON.stringify(url)  
  158.                 insertImg(url)  
  159.             } else {  
  160.                 this.$Message.error(result.msg);  
  161.             }  
  162.             // }  
  163.           }  
  164.         }  
  165.         this.editor.customConfig.onchange = (html) => {  
  166.           this.info_ = html // 綁定當前逐漸地值  
  167.           this.$emit('change'this.info_) // 將內容同步到父組件中  
  168.         }  
  169.         // 創建富文本編輯器  
  170.         this.editor.create()  
  171.       }  
  172.     }  
  173.   }  
  174. </script>  
  175.   
  176. <style lang="css">  
  177.   .editor {  
  178.     width: 100%;  
  179.     margin: 0 auto;  
  180.     position: relative;  
  181.     z-index: 0;  
  182.   }  
  183.   .toolbar {  
  184.     border: 1px solid #ccc;  
  185.   }  
  186.   .text {  
  187.     border: 1px solid #ccc;  
  188.     min-height: 500px;  
  189.   }  
  190. </style>  

4.調用組件的內容

 

  1. <template>  
  2. <div>  
  3.     <editor-bar v-model="detail" :isClear="isClear" @change="change"></editor-bar>  
  4. </div>  
  5. </template>  
  6. <script>  
  7. import EditorBar from '../../components/wangEnduit'  
  8. export default {  
  9.     components: { EditorBar },  
  10.     data() {  
  11.       return {  
  12.             test:'',  
  13.             isClear: false,  
  14.             detail:""  
  15.         }  
  16.       },    
  17.     methods: {  
  18.         change(val) {  
  19.             console.log(val)  
  20.         },  
  21.     }  
  22. }  
  23. </script>  

 5.效果圖

 

 

參考鏈接:https://www.cnblogs.com/huge1122/p/11346115.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM