vue富文字外掛vue-quill-editor的使用
阿新 • • 發佈:2018-12-30
- 首先下載vue-quill-editor cnpm install vue-quill-editor --save
- 新建公共元件quillEditor.vue,在quillEditor.vue中的html程式碼如下:<template><quill-editor v-model="content" ref="myTextEditor" :options="editorOption" @change="onChange" placeholder="111"> <div id="toolbar" slot="toolbar"> <span class
- 在quillEditor.vue中引入相應檔案<script>import { quillEditor } from "vue-quill-editor";import "quill/dist/quill.core.css";import "quill/dist/quill.snow.css";import "quill/dist/quill.bubble.css";import { async } from "@/config/fetch";export default { components: { quillEditor }, props: { /*編輯器的內容*/ value: { type: String }, /*圖片大小*/ maxSize: { type: Number, default: 400 //kb } }, data() { return { content: "", editorOption: { modules: { toolbar: "#toolbar" }, placeholder: "您想說點什麼?" } }; }, methods: { onChange() { this.$emit("input", this.content); },
/*裁切上傳成功 res根據上傳介面值獲取*/ onUploadSuccess: function(res) { this.editor.focus(); this.editor.insertEmbed( this.editor.getSelection().index, "image", res.url ); }, /*點選上傳圖片按鈕*/ fileClick(type) { var _this = this; /*建立input file*/ var input = document.createElement("input"); input.type = "file"; if (type == "image") { input.accept = "image/jpeg,image/png,image/jpg,image/gif"; } else { input.accept = "video/mp4,video/mpeg,video/3gpp,image/gif"; } input.onchange = onFileChange; input.click();
/*選擇上傳圖片/視訊切換*/ function onFileChange(e) { var fileInput = e.target; console.log(fileInput); if (fileInput.files.length == 0) { return; } console.log(fileInput.files[0]); var data = new FormData(); data.append("files", fileInput.files[0]); async("/newsController/uploadNewsFile.do", data, "FORM", { contentType: "" }) .then(data => { var red = data.data[0]; _this.editor.focus(); if (type == "image") { _this.editor.insertEmbed( _this.editor.getSelection().index, "image", red.compressPhotoUrl ); } else { _this.editor.insertEmbed( _this.editor.getSelection().index, "video", red.photoUrl ); } }) .catch(function(err) { console.log(err); _this.$notify.error({ title: "錯誤", message: "上傳失敗" }); }); } } }, computed: { editor() { return this.$refs.myTextEditor.quill; } },
mounted() { this.content = this.value; }, watch: { value(newVal, oldVal) { if (this.editor) { if (newVal !== this.content) { this.content = newVal; } } } }};</script> - 在quillEditor.vue中的樣式如下<style>.ql-picker-label { display: block !important; line-height: 24px;}.ql-picker-label::before { display: block !important;}.ql-tooltip { z-index: 100;}.ql-action::after { content: "儲存" !important;}.ql-tooltip[data-mode="link"]::before { content: "輸入連結:" !important;}.ql-tooltip[data-mode="video"]::before { content: "輸入視訊:" !important;}</style>
- 在頁面中使用quillEditor.vue這個元件
- html<el-form-item label="內容"> <quill-editor v-model="newsContent"></quill-editor></el-form-item>
- js<script>import quillEditor from "@/components/common/quillEditor";export default { components: { quillEditor }, data() { return { newsContent: "" }; }, mounted() {}};</script>