vue中使用vue-quill-editor,包括代码高亮以及图片上传到本地服务

安装cnpm install vue-quill-editor --save

在main.js里面引入

import VueQuillEditor from 'vue-quill-editor'
// require styles这里是富文本编辑器的样式引用
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
Vue.use(VueQuillEditor, /* { default global options } */)

在组件里面使用 editorOption为配置项,并上传到本地 安装vue-quill-editor-upload并在组件里引入import {quillRedefine} from 'vue-quill-editor-upload'

 components: {
    quillRedefine
  },
<quill-editor v-model="ruleForm.content"
                              ref="myQuillEditor"
                              :options="editorOption">
                        </quill-editor>
this.editorOption = quillRedefine(
        {
          // 图片上传的设置
          uploadConfig: {
            action:  '/blogUsers/image',  // 必填参数 图片上传地址,这里的后台是node
            // 必选参数  res是一个函数,函数接收的response为上传成功时服务器返回的数据
            // 你必须把返回的数据中所包含的图片地址 return 回去
            res: (respnse) => {
              console.log(respnse.msg.img.path, 'respnse.msg.img.path');
               var w=respnse.msg.img.path.indexOf('upload');
              // 这里切记要return回你的图片地址
               return 'http://localhost:3000/images/' + respnse.msg.img.path.substring(w);   
            }
          },
          theme:'snow'//这个是组题
        }
      ),

代码高亮 安装cnpm install highlight.js --save并在组件里面引入import hljs from 'highlight.js',然后在配置项里面加入:

this.editorOption.modules.syntax = {
          highlight: text => hljs.highlightAuto(text).value
      }

整体配置项如下

created  () {
     this.editorOption = quillRedefine(
        {
          // 图片上传的设置
          uploadConfig: {
            action:  '/blogUsers/image',  // 必填参数 图片上传地址
            // 必选参数  res是一个函数,函数接收的response为上传成功时服务器返回的数据
            // 你必须把返回的数据中所包含的图片地址 return 回去
            res: (respnse) => {
              console.log(respnse.msg.img.path, 'respnse.msg.img.path');
               var w=respnse.msg.img.path.indexOf('upload');
               // 这里切记要return回你的图片地址
               return 'http://localhost:3000/images/' + respnse.msg.img.path.substring(w);   
            }
          },
          theme:'snow'
        }
      ),
      this.editorOption.modules.syntax = {
          highlight: text => hljs.highlightAuto(text).value
      }
      console.log(this.editorOption, 'this.editorOption');
  }

获取回显的数据

<code class="hljs" v-html="ruleForm.content"></code>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 从18年7月16号入职到现在已经5个月多了,入职之后就一直使用vue,来这边温习记录下使用的步骤。在我的理解中vu...
    拖孩阅读 1,942评论 2 12
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    你猜_3214阅读 11,135评论 0 118
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    王喂马_阅读 6,473评论 1 77
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    小姜先森o0O阅读 9,602评论 0 72
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    柴东啊阅读 15,876评论 2 140