下载插件

npm 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)

在页面里使用

<quill-editor v-model="detail"
	:options="options"
	ref="QuillEditor"
	style="line-height: normal; width: 600px; height: 250px">
</quill-editor>
<el-upload
	style="display: none"
	class="avatar-uploader"
	:action="action"
	:show-file-list="false"
	:on-success="editImg" >
	<el-button size="small" type="primary">点击上传</el-button>
</el-upload>

js

import Quill from "vue-quill-editor";
let fonts = [
  "SimSun",
  "SimHei",
  "Microsoft-YaHei",
  "KaiTi",
  "FangSong",
  "Arial",
  "Times-New-Roman",
  "sans-serif",
];
Quill.Quill.imports["formats/font"].whitelist = fonts;
Quill.Quill.register(Quill.Quill.imports["formats/font"]);
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
  ["blockquote", "code-block"], // 引用  代码块
  // [{ header: 1 }, { header: 2 }], // 1、2 级标题
  [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
  [{ script: "sub" }, { script: "super" }], // 上标/下标
  [{ indent: "-1" }, { indent: "+1" }], // 缩进
  //需修改js和css文件
  [{ size: ["12", false, "16", "18", "20", "24", "28", "32", "36"] }], // 字体大小
  [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
  [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
  [{ font: fonts }], // 字体种类
  [{ align: [] }], // 对齐方式
  ["clean"], // 清除文本格式
  ["image"], // 链接、图片、视频
];
export default {
  data() {
    return {
      options: {
        placeholder: "",
        theme: "snow", // or 'bubble'
        placeholder: "请输入学校简介",
        modules: {
          toolbar: {
            container: toolbarOptions,
            handlers: {
              image: function (value) {
                if (value) {
                  document.querySelector(".avatar-uploader .el-button").click();
                } else {
                  this.quill.format("image", false);
                }
              },
            },
          },
        },
      },
    };
  },
  methods: {
		//富文本图片
	    editImg(res) {
	      let quill = this.$refs.QuillEditor.quill;
	      let length = quill.getSelection().index;
	      quill.insertEmbed(length, "image", res.message);
	      quill.setSelection(length + 1);
	    },
	}
 }
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐