vue+springboot前後端分離整合ueditor,避坑之圖片上傳出錯、無法回顯
阿新 • • 發佈:2020-11-23
1.前端vue搭建
首先不管vue-cli 2.0還是vue-cli 3.0你得會新建一個vue腳手架專案,讓它能夠正常執行起來
2.整合vue元件
npm i vue-ueditor-wrap -D 先安裝一下 vue-ueditor-wrap 外掛,我沒試過不安裝會是什麼效果,估計會導致沒法呼叫元件吧,可以理解為安裝驅動包。
不管你從網上或者官方原始碼,你得獲取一個ueditor的開發原始碼包,像下圖這樣的結構,vue-cli 2.0可以將ueditor資料夾放到static資料夾下,vue-cli 3.0可以將ueditor資料夾放到public資料夾下
image.png
App.vue程式碼
<div id="app"> <vue-ueditor-wrap v-model="msg" :config="myConfig" ref="myueditor" @ready="ready"></vue-ueditor-wrap> <div @click="getContent">獲取編輯器內容</div> </div> </template> <script> import VueUeditorWrap from 'vue-ueditor-wrap' export default { name: 'App', components: { VueUeditorWrap }, data(){ return{ msg: '437476', myConfig: { // 編輯器不自動被內容撐高 autoHeightEnabled: false, // 初始容器高度 initialFrameHeight: 240, // 初始容器寬度 initialFrameWidth: '100%', // 上傳檔案介面(檔案上傳功能搭建的後端介面) serverUrl: 'http://localhost:8084/live/editor/config', // UEDITOR_HOME_URL: process.env.BASE_URL + 'UEditor/' //專案內部相對路徑 可以找到我們引入的ueditor資料夾下的資源 UEDITOR_HOME_URL:'/UEditor/' }, editorInstance: null } }, methods:{ ready(editorInstance) { console.log(`編輯器例項${editorInstance.key}: `, editorInstance) this.editorInstance = editorInstance }, getContent() { console.log(this.msg) console.log(this.$refs.myueditor) } } } </script>
這是我們就可以訪問執行起來的vue專案,但是前提是你把serverUrl:這個介面能返回config.json資料
image.png
注意: 由於整合ueditor需要載入一個config.json的配置檔案,裡面定義了一些功能配置項,必須先獲取裡面的json,我們才能正常使用富文字的功能,不然上傳單張圖片圖示會被置灰,還有上傳多張圖片功能也不能使用,顯示“沒有配置後臺資料、、、、”,所有前端所有請求只會通過 serverUrl: 'http://localhost:8084/live/editor/config'這個介面發起,根據引數來區分各個功能。
3.後端