这是《基于nuxt和iview搭建OM后台管理系统实践》这一个系列文章的目录,大体思路以下:css
上一篇简要介绍了一下这个项目的项目背景,从这一篇开始我会写开发公共组件的过程,这一篇讲解一下富文本编辑器quill的集成吧。html
如动图所示,为后台管理系统添加内容的功能页面,能够看到已经集成了上传图片组件和富文本编辑器组件。前端
这个富文本集成了quill这个开源库 [[quill中文文档]](https://github.com/BingKui/Qu...。在vue-cli构建的项目中直接引用quill的包一点问题都没有,可是我用的nuxt.js是基于服务端渲染的,多数状况下会报下面这个错误:vue
window is not defined
这是由于window对象只存在于浏览器端,服务端渲染的时候是不存在window对象的。那么我应该怎么作呢??ios
我仍是直接上代码吧:git
// 文件plugins/nuxt-quill-plugins.js import Vue from 'vue' // import VueQuillEditor from 'vue-quill-editor' // Vue.use(VueQuillEditor) 直接引用会报错 if (process.browser) { //加一个浏览器端判断,只在浏览器端才渲染就不会报错了 const VueQuillEditor = require('vue-quill-editor/dist/ssr') Vue.use(VueQuillEditor) }
//文件nuxt.config.js,省略其余代码 plugins: [ '~plugins/iview-ui', '~plugins/qs', '~plugins/urlencode', {src: '~plugins/nuxt-quill-plugin.js',ssr: false} ],
//文件 components/full-editor.vue <template> <section class="container"> <!-- <form id="uploadForm"> --> <input class="file" type="file" style="display:none" id="file" ref="input" @change="doUpload"> <!-- </form> --> <div class="quill-editor" ref="myQuillEditor" :content="content" @change="onEditorChange($event)" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @ready="onEditorReady($event)" v-quill:myQuillEditor="editorOption"> </div> </section> </template> <script> import {qiniuConfig,quillEditorConfig} from '~/config';//七牛上传和富文本toolbar配置文件 const QiniuUPToken = require('qiniu-uptoken');//前端生成七牛上传token import axios from '~/plugins/axios'; export default { name: "full-editor", head() { return { link: [ { href: "/full-editor/quill.core.css", rel: "stylesheet" }, { href: "/full-editor/quill.snow.css", rel: "stylesheet" }, { href: "/full-editor/quill.bubble.css", rel: "stylesheet" } ] }; }, data() { const self = this; return { content: "", editorOption: { // some quill options modules: { toolbar: { container:quillEditorConfig.toolbarOptions, handlers:{ 'image':function(){ // console.log(this) this.quill.format('image', false);//禁用quill内部上传图片方法 self.imgHandler(this) } } }, }, placeholder: '请输入信息', theme: "snow", quill:'' } }; }, mounted() { // console.log("app init"); }, methods: { onEditorBlur(editor) { // console.log("editor blur!", editor); }, onEditorFocus(editor) { // console.log("editor focus!", editor); }, onEditorReady(editor) { // console.log("editor ready!", editor); }, onEditorChange({ editor, html, text }) { // console.log("editor change!", editor, html, text); this.content = html; this.$emit('editorContent',html) // console.log(this.content); }, imgHandler(handle){ this.quill = handle.quill; var inputfile = document.getElementById('file'); inputfile.click(); }, doUpload(){ let files = document.getElementById('file'); // console.log(files.files[0]); let uptoken = QiniuUPToken(qiniuConfig.access_key,qiniuConfig.secret_key,qiniuConfig.bucketname) // console.log(uptoken); this.qiniuUpload(files.files[0],uptoken); }, qiniuUpload(file, token){ let param = new FormData(); //建立form对象 param.append('file',file,file.name);//经过append向form对象添加数据 param.append('token',token);//添加form表单中其余数据 // console.log(param. `get('file')); //FormData私有类对象,访问不到,能够经过get判断值是否传进去 let config = { headers:{'Content-Type':'multipart/form-data'} }; //添加请求头 axios.post(qiniuConfig.action_url,param,config) .then(res=>{ // console.log(res); // console.log(this.quill); let length = this.quill.getSelection().index; const imgUrl = qiniuConfig.pic_hostname+res.key;//插入上传的图片 this.quill.insertEmbed(length, 'image', imgUrl); // this.quill.insertEmbed(index, 'image', imgUrl);//插入上传的图片 // console.log(res.data); }) .then((err)=>{ // console.log(err) }) } } }; </script> <style scoped> .container { width: 100%; margin: 0 auto; /* padding: 10px 0; */ } .quill-editor { min-height: 200px; max-height: 400px; overflow-y: auto; } </style>
封装组件须要注意的几个点:github
<script> export default { head() { return { link: [ { href: "/full-editor/quill.core.css", rel: "stylesheet" }, { href: "/full-editor/quill.snow.css", rel: "stylesheet" }, { href: "/full-editor/quill.bubble.css", rel: "stylesheet" } ] }; }, } </script>
// 文件config/index.js /** * @description 富文本编辑器quill的配置文件 * @argument 参考文档https://sheweifan.github.io/2018/01/07/nuxt-quill-qiniu/ * @argument quill中文文档https://github.com/BingKui/QuillChineseDoc/blob/master/SUMMARY.md */ export const quillEditorConfig = { toolbarOptions:[ ["bold", "italic", "underline", "strike"], // 切换按钮 ["blockquote", "code-block"], // [{ header: 1 }, { header: 2 }], // 用户自定义按钮值 [{ list: "ordered" }, { list: "bullet" }], [{ script: "sub" }, { script: "super" }], // 上标/下标 [{ indent: "-1" }, { indent: "+1" }], // 减小缩进/缩进 [{ direction: "rtl" }], // 文本下划线 [{ size: ["small", false, "large", "huge"] }], // 用户自定义下拉 [{ header: [1, 2, 3, 4, 5, 6, false] }], [{ color: [] }, { background: [] }], // 主题默认下拉,使用主题提供的值 [{ font: [] }], [{ align: [] }], ['image'],//上传图片 ['video'],//视频 ["clean"] // 清除格式 ] }
// 父组件 <template> <div class="body"> <full-editor ref="myFullEditor" v-model="formItem.body" @editorContent="editorContent" > </full-editor> </div> </template> <script> const fullEditor = () => import("@/components/full-editor"); export default { layout: "nav", components: { fullEditor }, mounted() { this.loadData();//加载数据 this.$refs.myFullEditor.content = this.body;//父组件给富文本编辑器传递值 } } </script>
封装一个富文本组件,开始作以前觉得会蛮容易的,觉得就引用一下就就能够了,没想到会遇到以上的那些坑,最终在百度和翻阅github后很好的解决了问题,最终也封装完成也知足了需求,后续我会找个时间剔除一些业务代码把组件放到github上。vue-cli