element源码学习笔记

文章介绍了ElementUI源码中的一些编程技巧,如条件语句的简写,向数组开头添加元素的unshift方法,判断是否为数组的Array.isArray,访问父组件数据的$parent,动态类名的绑定以及事件监听的添加和移除。同时,提到了v-clickoutside指令和组件间通信的v-bind及inject的使用。
摘要由CSDN通过智能技术生成

element源码学习笔记
(1)方法简写

 if (!layout) {
      return null
    }
    // 如果if大括号里只有单行,可以去除大括号
    if (!layout) return null;
    // else只有单行的2时候也可以去除大括号
const a = 7; if (a > 9) console.log(1); else console.log(2); 

(2) 向数组开头添加新项,unshift()

template.children.unshift(rightWrapper);

(3)判断是否是数组Array.isArray(newVal)

if (Array.isArray(newVal)) {
              this.$parent.internalPageSize = newVal.indexOf(this.$parent.pageSize) > -1
                ? this.$parent.pageSize
                : this.pageSizes[0];
            }

(4)取父组件和子组的data,props ,方法,this.$parent

(5)动态样式:
:class=“[‘el-dialog’, { ‘is-fullscreen’: fullscreen, ‘el-dialog–center’: center }, customClass]”
解析: 该div始终有el-dialog,customClass,当fullscreen和center为true时,is-fullscreen,el-dialog–center存在

 <div
        role="dialog"
        :key="key"
        aria-modal="true"
        :aria-label="title || 'dialog'"
        :class="['el-dialog', { 'is-fullscreen': fullscreen, 'el-dialog--center': center }, customClass]"
        ref="dialog"
        :style="style">

(6)

this.$el.addEventListener('scroll', this.updatePopper), this.$el.removeEventListener('scroll', this.updatePopper);

知识点1: $el :是 用于获取组件内 DOM(包括子组件,当前.vue组件,以及父组件)
知识点2:addEventListener:添加事件监听,removeEventListener:移除事件监听

(7)v-clickoutside=“close” 点击元素之外,触发事件
参考: 优秀博主具体讲解

(8)v-bind=“[$props, $attrs]”, 组件传值
参考:

优秀博主讲解

(9)inject

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值