在vue2.0和3.0cli版本中使用quill-editor編輯器
在vue cli2.0中使用碉咆,
1.首先下載npm包,下載到開發(fā)環(huán)境
$npm install vue-quill-edito -S
$npm install quill -S
$npm install quill-image-resize-module -S // 圖片自由拖動改變大小
如圖
微信圖片_20191121145129.png
2.在頁面中引入毛甲,最好封裝成一個組件勒极,這里有個更改他的圖片上傳功能,上傳為服務(wù)器的api矛绘,返回一個url地址耍休。這里還涉及到一個圖片自由改變大小的組件quill-image-resize-module,
<template>
<!-- 這是一個富文本的組件-->
<div class="editor_wrap">
<el-upload
class="avatar-uploader"
action=""
name="img"
:show-file-list="false"
:http-request="uploadQuillImage"
:on-success="uploadQuillSuccess"
:on-error="uploadQuillError"
:before-upload="beforeQuillrUpload">
</el-upload>
<el-row v-loading="quillUpdateImg">
<quill-editor
v-model="content"
ref="myQuillEditor"
:options="editorOption"
@blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
@change="onEditorChange($event)">
</quill-editor>
</el-row>
</div>
</template>
<script>
import upload from '../../utils/api/index' // 這里我封裝的一個上傳圖片的货矮,返回一個url地址羊精,將它插入光標位置
import Quill from 'quill' //引入編輯器
import {quillEditor} from "vue-quill-editor";
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
// 自定義字體大小
let Size = Quill.import('attributors/style/size')
Size.whitelist = ['10px', '12px', '14px', '16px', '18px', '20px', '24px', '32px']
Quill.register(Size, true)
//quill編輯器的字體
var fonts = ['SimSun', 'SimHei', 'Microsoft-YaHei', 'KaiTi', 'FangSong', 'Arial', 'Times-New-Roman', 'sans-serif'];
var Font = Quill.import('formats/font');
Font.whitelist = fonts; //將字體加入到白名單
Quill.register(Font, true);
//quill圖片可拖拽改變大小
import ImageResize from 'quill-image-resize-module'
Quill.register('modules/imageResize', ImageResize)
// 富文本工具欄配置
const toolbarOptions = [
['bold', 'italic', 'underline', 'strike'], // toggled buttons
['blockquote', 'code-block'],
[{'header': 1}, {'header': 2}], // custom button values
[{'list': 'ordered'}, {'list': 'bullet'}],
[{'script': 'sub'}, {'script': 'super'}], // superscript/subscript
[{'indent': '-1'}, {'indent': '+1'}], // outdent/indent
[{'direction': 'rtl'}], // text direction
[{'size': Size.whitelist}], // custom dropdown
[{'header': [1, 2, 3, 4, 5, 6, false]}],
[{'color': []}, {'background': []}], // dropdown with defaults from theme
[{'font': fonts}],
[{'align': []}],
['link', 'image', 'video'],
['clean'] // remove formatting button
];
export default {
name: "quillEditorCom",
data() {
return {
content: this.quillContent, 我們不能直接使用props傳過來的值,先賦值到這里
// 富文本配置項
quillUpdateImg: false, // 根據(jù)圖片上傳狀態(tài)來確定是否顯示loading動畫囚玫,剛開始是false,不顯示
editorOption: {
placeholder: '',
theme: 'snow', // or 'bubble'
modules: {
imageResize: { //調(diào)整大小組件喧锦。
displayStyles: {
backgroundColor: 'black',
border: 'none',
color: 'white'
},
modules: ['Resize', 'DisplaySize', 'Toolbar']
},
toolbar: {
container: toolbarOptions, // 工具欄
handlers: {
'image': function (value) {
if (value) {
// 觸發(fā)input框選擇圖片文件
document.querySelector('.avatar-uploader input').click()
} else {
this.quill.format('image', false);
}
}
}
}
}
},
}
},
props: ['quillContent'],
components: {quillEditor},
computed: {
editor() {
return this.$refs.myQuillEditor.quill;
},
},
methods: {
uploadQuillImage: function (e) {
這是上傳圖片的函數(shù),可以改成自己的抓督,成功返回一個地址插入光標處
let that = this;
// 獲取富文本組件實例
let quill = this.$refs.myQuillEditor.quill
let func_s = function (data) {
that.$message({
message: '上傳成功',
type: 'success'
});
// 獲取光標所在位置
let length = quill.getSelection().index;
// 插入圖片 data.url為服務(wù)器返回的圖片地址
quill.insertEmbed(length, 'image', data.url)
// 調(diào)整光標到最后
quill.setSelection(length + 1)
};
let func_f = function (err) {
that.$message.error('上傳失敗');
};
// loading動畫消失
this.quillUpdateImg = false
upload.upload(e, func_s, func_f);
},
beforeQuillrUpload: function (file) {
// 顯示loading動畫
this.quillUpdateImg = true】
這是我封裝的一個判斷是否上傳為圖片燃少,圖片大小的公共函數(shù),自己可自定義
Utils.base.beforeAvatarUpload(file);
},
// 成功失敗回調(diào)
uploadQuillSuccess() {
},
uploadQuillError() {
// loading動畫消失
this.quillUpdateImg = false
this.$message.error('圖片插入失敗')
},
onEditorReady(editor) { // 準備編輯器
},
onEditorBlur() {
}, // 失去焦點事件
onEditorFocus() {
console.log(this.$refs.myQuillEditor.quill.getSelection().index,'獲取示例')
}, // 獲得焦點事件
onEditorChange() {
this.$emit('changeQuill', this.content)//將值綁定到changeQuill上傳遞過去,引入組件的時候監(jiān)聽這個值铃在,可以拿到改變的值阵具,
}, // 內(nèi)容改變事件
}
}
</script>
<style scoped lang="scss">
.ql-toolbar {
white-space: normal;
}
.editor_wrap /deep/ .avatar-uploader {
display: none;
}
.editor_wrap /deep/ .editor {
line-height: normal !important;
height: 270px;
margin-bottom: 60px;
}
.editor_wrap /deep/ .editor .ql-bubble .ql-editor a {
color: #136ec2;
}
.editor_wrap /deep/ .editor img {
max-width: 720px;
margin: 10px;
}
.ql-snow .ql-tooltip[data-mode=link]::before {
content: "請輸入鏈接地址:";
}
.ql-snow .ql-tooltip.ql-editing a.ql-action::after {
border-right: 0px;
content: '保存';
padding-right: 0px;
}
.editor_wrap {
}
</style>
當你引入這個圖片自由改變大小的組件后可能活報錯 imports 的錯,這時我們的需要在配置項中配置quill定铜,下面先介紹vuecli2.0中配置阳液,找到build文件中的 webpack.base.conf.js 文件,加上這段
module.exports = {
plugins: [
new webpack.ProvidePlugin({
// 這是富文本編輯器的
'window.Quill': 'quill/dist/quill.js',
'Quill': 'quill/dist/quill.js'
})
]
}
vue cli3.0中在 vue.config.js 中配置
module.exports = {
configureWebpack: {
// 把原本需要寫在webpack.config.js中的配置代碼 寫在這里 會自動合并
plugins: [
new webpack.ProvidePlugin({
'window.Quill': 'quill/dist/quill.js',
'Quill': 'quill/dist/quill.js'
})
]
}
}
重新啟動就不會報錯了組件引入正常使用了