vue-quill-editor副文本编辑器自定义工具栏内的工具及新增工具按钮绑定事件调用vue方法

如果没有安装 先npm

 npm install vue-quill-editor --save

引入

import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

初始化编辑器

<template>
  <quill-editor ref="myTextEditor" v-model="content" :options="quillOption">
  </quill-editor>
</template>

配置

export default {
  components: {
    quillEditor,
  },
  mounted() {
    this.quillOption.initButton();
  },
  data() {
    return {
      content: "<h2>freddy</h2>",
      quillOption: {
        theme: "snow", // 主题
        placeholder: "",
        modules: {
          toolbar: {
            container: [
              ["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: [] }],
              ["clean", "link", "image", "video"],
              ["addBtn"],      //新添加的工具
            ], // 自定义工具栏选项
            
            handlers: {   
              addBtn: () => {    //新增按钮绑定事件 自定义  调用vue实例上的method   这里注意名字要和上面新增按钮的名字一样
                this.btnMethod();
              },
            },
          },
        },
        initButton: function () {
          //在使用的页面中初始化按钮样式
          const addBtn= document.querySelector(".ql-addBtn");
          addBtn.style.cssText =
            "width:40px; border:1px solid #ccc; border-radius:5px;font-size:20px;padding-bottom:30px;";
          addBtn.className = "iconfont icon-reset";


        },
      },
    };
  },

  methods: {
    btnMethod() {
      console.log(this);
    },
  },
};
要在vue-quill-editor文本编辑器自定义工具栏内添加多个自定义按钮,你可以按照以下步骤进行操作: 1. 首先,将vue-quill-editor引入到项目中。可以使用以下代码: ```javascript import { quillEditor } from 'vue-quill-editor' ``` 2. 在组件中设置编辑器的相关数据和选项。可以使用以下代码: ```javascript export default { data() { return { content: null, editorOption: { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], // 默认按钮 ['blockquote', 'code-block'], // 默认按钮 ['custom_button_1', 'custom_button_2'] // 自定义按钮 ] } } } }, methods: { onEditorBlur() { // 失去焦点事件 }, onEditorFocus() { // 获得焦点事件 }, onEditorChange() { // 内容改变事件 } } } ``` 3. 在toolbar中添加自定义按钮。在`toolbar`数组中添加一个新的数组,该数组包含你想要添加的自定义按钮的标识符。例如,你可以添加`custom_button_1`和`custom_button_2`两个自定义按钮。 4. 在编辑器的模板中使用`quill-editor`组件,并将之前设置的`content`、`editorOption`和相关事件绑定到相应的属性上。可以使用以下代码: ```html <template> <quill-editor v-model="content" ref="myQuillEditor" :options="editorOption" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @change="onEditorChange($event)"></quill-editor> </template> ``` 通过以上步骤,你就可以在vue-quill-editor文本编辑器自定义工具栏中添加多个自定义按钮了。请注意,在`toolbar`数组中的每个子数组都代表一行按钮,你可以根据需要添加更多的按钮,并按照自己的需求进行自定义
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值