Vue-封装-$on $emit $off(封装事件订阅)

本文详细介绍了如何在Vue应用中封装和使用$on、$emit和$off事件管理方法,提升组件间的通信效率。通过自定义事件中心,实现事件订阅、发布和取消订阅,帮助开发者更好地控制组件间的交互和状态管理。
摘要由CSDN通过智能技术生成
/*
    事件池
    eventList = {
         handle:[fn1,fn2,fn3]
         abc:[fn1,fn2,fn3]
    }
    ||
    eventList = {
      
    }

    $on(eventName,callback)
    判断当前事件名称是否存在,如果不存在则创建一个key值为事件名称  value为一个数组
    将callback push到数组中

    如果事件名称存在则直接添加到数组中去


    $emit(eventName,[params])

    判断当前事件名称是否存在,如果存在则遍历数组,得到所有的函数,并执行。然后将params当做实参传递到函数中去


    $off(eventName,[callback])


    判断当前事件名称是否存在,如果存在继续判断第二个参数是否存在,如果存在则找到相对于的下标 然后将函数在数组中移除
    如果不存在则将整个数组清空
*/

const eventList = {}


const $on = (eventName,callback)=>{
    if(!eventList[eventName]){
        eventList[eventName] = [];
    }
    eventList[eventName].push(callback)
}



const $emit = (eventName,params)=>{
    if(eventList[eventName]){
        let arr = eventList[eventName];
        arr.map((cb)=>{
            cb(params);
        })
    }

}

const $off = (eventName,callback)=>{
    if(eventList[even
Vue2 封装 Quill 可以让我们在 Vue2 项目中更方便地使用 Quill 富文本编辑器。 以下是一个简单的 Vue2 + Quill 的组件封装示例: 1. 安装 Quill ``` npm install quill --save ``` 2. 创建 QuillEditor.vue 组件 ```vue <template> <div ref="editor"></div> </template> <script> import Quill from 'quill' export default { name: 'QuillEditor', props: { value: String, // 绑定的值 options: Object // Quill 配置项 }, mounted () { this.initEditor() }, methods: { initEditor () { const options = this.options || { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], // 加粗、斜体、下划线、删除线 [{ 'header': 1 }, { 'header': 2 }], // 标题,键值对的形式;1、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'] // 清除文本格式 ] }, placeholder: '请输入内容', theme: 'snow' // 主题 } this.editor = new Quill(this.$refs.editor, options) this.editor.on('text-change', this.handleTextChange) this.editor.root.innerHTML = this.value }, handleTextChange () { this.$emit('input', this.editor.root.innerHTML) } }, beforeDestroy () { this.editor.off('text-change', this.handleTextChange) this.editor = null } } </script> ``` 3. 在父组件中使用 ```vue <template> <div> <quill-editor v-model="content"></quill-editor> </div> </template> <script> import QuillEditor from './components/QuillEditor.vue' export default { components: { QuillEditor }, data () { return { content: '' } } } </script> ``` 在这个示例中,我们使用了 `QuillEditor.vue` 组件来封装 Quill 富文本编辑器,并且使用了 `v-model` 来实现数据的双向绑定。同时,我们也可以通过 `options` 属性来传递 Quill 的配置项,以达到个性化定制的目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值