Vue+Element UI+vue-quill-editor富文本編輯器及插入圖片自定義
本文為大家分享了Vue+Element UI+vue-quill-editor富文本編輯器及插入圖片自定義,供大家參考,具體內(nèi)容如下
1.安裝
npm install vue-quill-editor --save
2.在main.js中引入
import VueQuillEditor from 'vue-quill-editor' import 'quill/dist/quill.core.css' import 'quill/dist/quill.snow.css' import 'quill/dist/quill.bubble.css' Vue.use(VueQuillEditor);
3. template
<div> <!-- 圖片上傳組件輔助--> <el-upload class="avatar-uploader" :action="serverUrl" name="img" :headers="header" :show-file-list="false" :on-success="uploadSuccess" :on-error="uploadError" :before-upload="beforeUpload"> </el-upload> <quill-editor v-model="content" ref="myQuillEditor" :options="editorOption" @change="onEditorChange($event)" > </quill-editor> </div>
4.js
<script>
const toolbarOptions = [
['bold', 'italic', 'underline', 'strike'], // toggled buttons
[{'header': 1}, {'header': 2}], // custom button values
[{'list': 'ordered'}, {'list': 'bullet'}],
[{'indent': '-1'}, {'indent': '+1'}], // outdent/indent
[{'direction': 'rtl'}], // text direction
[{'size': ['small', false, 'large', 'huge']}], // custom dropdown
[{'header': [1, 2, 3, 4, 5, 6, false]}],
[{'color': []}, {'background': []}], // dropdown with defaults from theme
[{'font': []}],
[{'align': []}],
['link', 'image'],
['clean']
]
export default {
data() {
return {
quillUpdateImg: false, // 根據(jù)圖片上傳狀態(tài)來確定是否顯示loading動(dòng)畫,剛開始是false,不顯示
content: null,
editorOption: {
placeholder: '',
theme: 'snow', // or 'bubble'
modules: {
toolbar: {
container: toolbarOptions,
handlers: {
'image': function (value) {
if (value) {
// 觸發(fā)input框選擇圖片文件
document.querySelector('.avatar-uploader input').click()
} else {
this.quill.format('image', false);
}
}
}
}
}
},
serverUrl: '/manager/common/imgUpload', // 這里寫你要上傳的圖片服務(wù)器地址
header: {
// token: sessionStorage.token
} // 有的圖片服務(wù)器要求請(qǐng)求頭需要有token
}
},
methods: {
onEditorChange({editor, html, text}) {//內(nèi)容改變事件
console.log("---內(nèi)容改變事件---")
this.content = html
console.log(html)
},
// 富文本圖片上傳前
beforeUpload() {
// 顯示loading動(dòng)畫
this.quillUpdateImg = true
},
uploadSuccess(res, file) {
// res為圖片服務(wù)器返回的數(shù)據(jù)
// 獲取富文本組件實(shí)例
console.log(res);
let quill = this.$refs.myQuillEditor.quill
// 如果上傳成功
if (res.code == 200 ) {
// 獲取光標(biāo)所在位置
let length = quill.getSelection().index;
// 插入圖片 res.url為服務(wù)器返回的圖片地址
quill.insertEmbed(length, 'image', res.url)
// 調(diào)整光標(biāo)到最后
quill.setSelection(length + 1)
} else {
this.$message.error('圖片插入失敗')
}
// loading動(dòng)畫消失
this.quillUpdateImg = false
},
// 富文本圖片上傳失敗
uploadError() {
// loading動(dòng)畫消失
this.quillUpdateImg = false
this.$message.error('圖片插入失敗')
}
}
}
注意:serverUrl :文件上傳地址不能直接寫全路徑,會(huì)出現(xiàn)跨域問題報(bào)錯(cuò)。需要在conf/index.js 中 進(jìn)行配置
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
host: 'localhost', // can be overwritten by process.env.HOST
port: 8088, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: true,
cssSourceMap: true,
proxyTable: {
'/api': {
target: 'http://localhost:18080/', //設(shè)置調(diào)用接口域名和端口號(hào)別忘了加http
changeOrigin: true,
pathRewrite: {
'^/api': '/' //這里理解成用‘/api'代替target里面的地址,組件中我們調(diào)接口時(shí)直接用/api代替
// 比如我要調(diào)用'http://0.0:300/user/add',直接寫‘/api/user/add'即可 代理后地址欄顯示/
}
},
'/manager': {
target: 'http://localhost:18081/',
changeOrigin: true,
pathRewrite: {
'^/manager': '/'
}
}
}
},
5.style
<style>
.ql-editor.ql-blank, .ql-editor {
height: 350px;
}
</style>
6.后臺(tái)圖片上傳接口
@RequestMapping(value = "/imgUpload")
public Map<String ,Object> imgUpload(HttpServletRequest req, MultipartHttpServletRequest multiReq)
throws IOException {
FileOutputStream fos = new FileOutputStream(
new File("E://fileupload//upload.jpg"));
FileInputStream fs = (FileInputStream) multiReq.getFile("img").getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fs.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
Map<String ,Object> map=new HashMap<>();
map.put("code",200);
map.put("msg","上傳成功");
map.put("url","http://localhost:8080/tomcat.png");
return map;//這里只做返回值測(cè)試用,url 參數(shù)為圖片上傳后訪問地址。具體根據(jù)功能進(jìn)行修改}
7.效果如下

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli或vue項(xiàng)目利用HBuilder打包成移動(dòng)端app操作
這篇文章主要介紹了vue-cli或vue項(xiàng)目利用HBuilder打包成移動(dòng)端app操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue中post請(qǐng)求報(bào)400的解決方案
這篇文章主要介紹了vue中post請(qǐng)求報(bào)400的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
詳解vue3中setUp和reactive函數(shù)的用法
這篇文章主要介紹了vue3函數(shù)setUp和reactive函數(shù)的相關(guān)知識(shí)及setup函數(shù)和reactive函數(shù)的注意點(diǎn),通過具體代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-06-06

