elment-ui使用vue-quill-editor富文本编辑器

  1. 全局配置

参考文档:https://www.kancloud.cn/liuwave/quill/1409423

(一)直接将配置信息写在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'


const 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': ['SimSun', 'SimHei', 'Microsoft-YaHei', 'KaiTi', 'FangSong', 'Arial'] }],// 字体
  [{ 'align': [] }], // 居中
  ['clean'],                                       // 清除样式,
  ['link', 'image'],  // 上传图片、上传视频
]

let editorOption={
  placeholder: '请在这里输入',
  theme: 'snow', //主题 snow/bubble
  modules: {
    history: {
      delay: 1000,
      maxStack: 50,
      userOnly: false
    },
    toolbar: {
      container: toolbarOptions,
      handlers: {
        image: function (value) {
          if (value) {
            // 调用element的图片上传组件
            document.querySelector('.avatar-uploader input').click()
          } else {
            this.quill.format('image', false)
          }
        }
      }
    }
  }
}
console.log(VueQuillEditor,editorOption)
Vue.use(VueQuillEditor,editorOption);

(二)页面内使用方式

//页面全局调用
<quill-editor v-model="subBanner.content"></quill-editor>
<el-upload class="avatar-uploader" action="" name="img" :show-file-list="false">
    </el-upload>

在用刀quill-editor组件时,需要配置一个类名为avatar-uploader的el-upload上传组件

  1. 组件封装(推荐使用这种方式)

(一)新增editText一个组件

<template>
  <div class="editText">
    <quill-editor v-model="content" :content="content" :options="editorOption" ref="QuillEditor">
    </quill-editor>

    <el-upload :on-success="imgSuccess" ref="edit" :action="submitFile" name="file" :show-file-list="false"></el-upload>


  </div>
</template>

<script>
const 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': ['SimSun', 'SimHei', 'Microsoft-YaHei', 'KaiTi', 'FangSong', 'Arial'] }],// 字体
  [{ 'align': [] }], // 居中
  ['clean'],                                       // 清除样式,
  ['link', 'image'],  // 上传图片、上传视频
]


export default {
  name: "editText",
  data(){
    return {
      content:this.value,
      fileList:[],
      editorOption: {
        placeholder: '请输入内容',
        theme: 'snow', //主题 snow/bubble
        modules: {
          toolbar: {
            container: toolbarOptions,
            handlers: {
              image:this.updataImg
            }
          }
        }
      },
    }
  },
  props:{
    value:{
      type:String,
      default:''
    }
  },
  methods:{
    updataImg(){
      //点击了上传图片按钮
      this.$refs["edit"].$el.children[0].click();
    },
    imgSuccess(response, file, fileList){
      if(response.code==200){
        // 获取富文本组件实例
        let quill = this.$refs.QuillEditor.quill;
        // 插入图片,res为服务器返回的图片链接地址
        quill.insertEmbed(length, 'image', this.filePath+response.data[0])
        // 调整光标到最后
        quill.setSelection(length + 1)
      }
      console.log(response);
    }
  },
  watch:{
    content(value){
      this.$emit('input',value);
    },
    value(val){
      this.content=val;
    }
  }
}
</script>

<style scoped>

</style>

(二) 调用方法

<template>
<div>
  <edit-text v-model="content"></edit-text>
</div>
</template>

<script>
import editText from "@/components/editText.vue";
export default {
  name: "fuwenben1Admin",
  components:{
    editText
  },
  data(){
    return {
      content:'',
    }
  }
}
</script>

<style scoped>

</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值