1.安裝wangEditor
npm install wangeditor
2.創建公共組件
在components中創建wangEnduit文件夾


3.編寫index.vue組件
- <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 'wangeditor/release/wangEditor.min.css'
- 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) {
- // 觸發清除文本域內容
- 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為編輯框輸入的內容,這里我監聽了一下值,當父組件調用得時候,如果給value賦值了,子組件將會顯示父組件賦給的值
- },
- mounted() {
- this.seteditor()
- this.editor.txt.html(this.value)
- },
- methods: {
- seteditor() {
- this.editor = new E(this.$refs.toolbar, this.$refs.editor)
- this.editor.customConfig.uploadImgShowBase64 = false // base 64 存儲圖片
- this.editor.customConfig.uploadImgServer = ''// 填寫配置服務器端地址
- this.editor.customConfig.uploadImgHeaders = { }// 自定義 header
- this.editor.customConfig.uploadFileName = 'file' // 后端接受上傳文件的參數名
- this.editor.customConfig.uploadImgParams = {
- // 如果版本 <=v3.1.0 ,屬性值會自動進行 encode ,此處無需 encode
- // 如果版本 >=v3.1.1 ,屬性值不會自動 encode ,如有需要自己手動 encode
- file_type: 'img'
- }
- this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024 // 將圖片大小限制為 2M
- this.editor.customConfig.uploadImgMaxLength = 6 // 限制一次最多上傳 6 張圖片
- this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000 // 設置超時時間
- // 自定義 onchange 觸發的延遲時間,默認為 200 ms
- // this.editor.customConfig.onchangeTimeout = 1000 // 單位 ms
- // 隱藏�網絡圖片�tab
- // this.editor.customConfig.showLinkImg = false
- // 表情面板可以有多個 tab ,因此要配置成一個數組。數組每個元素代表一個 tab 的配置
- // this.editor.customConfig.emotions = [
- // {
- // // tab 的標題
- // title: '默認',
- // // type -> 'emoji' / 'image'
- // type: 'image',
- // // content -> 數組
- // content: [
- // {
- // alt: '[壞笑]',
- // src: 'http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/50/pcmoren_huaixiao_org.png'
- // },
- // {
- // alt: '[舔屏]',
- // src: 'http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/40/pcmoren_tian_org.png'
- // },
- // {
- // alt: "[哈哈]",
- // src: "http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/8f/2018new_haha_org.png",
- // },
- // {
- // src : "http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/7a/shenshou_thumb.gif",
- // alt : "[草泥馬]"
- // }
- // ]
- // },
- // {
- // // tab 的標題
- // title: 'emoji',
- // // type -> 'emoji' / 'image'
- // type: 'emoji',
- // // content -> 數組
- // content: ['😀', '😃', '😄', '😁', '😆']
- // }
- // ],
- // 配置菜單
- 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) => {
- // 插入圖片失敗回調
- },
- success: (xhr, editor, result) => {
- // 圖片上傳成功回調
- },
- timeout: (xhr, editor) => {
- // 網絡超時的回調
- },
- error: (xhr, editor) => {
- // 圖片上傳錯誤的回調
- },
- customInsert: (insertImg, result, editor) => {
- // 圖片上傳成功,插入圖片的回調
- //result為上傳圖片成功的時候返回的數據,這里我打印了一下發現后台返回的是data:[{url:"路徑的形式"},...]
- // console.log(result.data[0].url)
- //insertImg()為插入圖片的函數
- //循環插入圖片
- // for (let i = 0; i < 1; i++) {
- if (result.code == 200) {
- let url = result.data.image_url
- JSON.stringify(url)
- insertImg(url)
- } else {
- this.$Message.error(result.msg);
- }
- // }
- }
- }
- 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: 500px;
- }
- </style>
4.調用組件的內容
- <template>
- <div>
- <editor-bar v-model="detail" :isClear="isClear" @change="change"></editor-bar>
- </div>
- </template>
- <script>
- import EditorBar from '../../components/wangEnduit'
- export default {
- components: { EditorBar },
- data() {
- return {
- test:'',
- isClear: false,
- detail:""
- }
- },
- methods: {
- change(val) {
- console.log(val)
- },
- }
- }
- </script>
5.效果圖