elementui选择卡片-Web Components如何在Element UI类中实现Card卡片

2023-08-23 0 6,148 百度已收录

大多数人都不太理解这篇《WebComponents如何在ElementUI中实现卡片》一文的知识点,所以小编为大家总结了以下内容。 内容详细,步骤清晰,具有一定的参考价值。 希望大家看完这篇文章后,有所收获。 接下来我们看一下《WebComponents如何在ElementUI类中实现Card卡》这篇文章。

WebComponents核心组成1.CustomElements

定义HTML标签elementui选择卡片,称为自定义元素(customelement)。 根据规范,自定义元素的名称必须包含连字符 -,用于区别于原生 HTML 元素。 因此,不能写。

  
       
  
         
           
  
  class ComCard extends HTMLElement {     constructor() {       super()       var tplEle = document.getElementById('custom-card')       this.append(tplEle)     }   }   window.customElements.define('com-card', ComCard)

这会注册一个自定义元素标签以进行浏览器感知渲染。

2. 影子 DOM

ShadowDOM 是 DOM 的封装。 标记结构、样式和行为可以隐藏并与页面上的其他代码隔离elementui选择卡片,以确保不同部分不会混合在一起,使代码更加干净整洁。

使用自定义元素的 this.attachShadow() 方法来启用 ShadowDOM。

class ComCard extends HTMLElement {
  constructor() {
    super()
    var shadow = this.attachShadow({mode: 'closed'})  // open
    var tplEle = document.getElementById('custom-card')
    shadow.appendChild(tplEle)
  }
}
window.customElements.define('com-card', ComCard);

参数{mode:'close'}表示ShadowDOM处于关闭状态,不允许外部访问。

3. 模板和插槽

由于组件的样式要和代码一起封装,所以只对自定义元素生效,不影响外部全局样式。 因此,可以在其上编写样式,以便可以多次重用它作为自定义元素结构的基础。

  
    .com-card {
    }
  
  
  
  class ComCard extends HTMLElement {     constructor() {       super();       var shadow = this.attachShadow({mode: 'closed'})  // open       var tplEle = document.getElementById('custom-card-template')       var content = tplEle.content.cloneNode(true)       shadow.appendChild(content)     }   }   window.customElements.define('com-card', ComCard);

完整代码



  
  Web Component
  
    * {
        box-sizing: border-box;
    }
    body {
        font-size: 14px;
    }
    .box {
        padding: 5px 0 30px;
    }
    .box .caption {
        display: none;
    }
    .box h2 {
        text-align: center;
    }
    .box li {
        color: #666;
        font-size: 14px;
        line-height: 1.8;
        margin-top: 15px;
    }
    .img {
        display: block;
        width: 80%;
        margin: 0 !important;
    }
    .card-head {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .card-title {
        color: #333;
        font-size: 16px;
    }
    .card-head-btn {
        color: #409eff;
        cursor: pointer;
        text-decoration: none !important;
    }
    .card-head-btn:hover {
        text-decoration: none;
    }
  


  

Web Component

       
      
卡片名称
      操作按钮     
          
  
       
      
卡片名称
      操作按钮     
         
          
  1. 君不见黄河之水天上来,奔流到海不复回。
  2.       
  3. 君不见高堂明镜悲白发,朝如青丝暮成雪。
  4.       
  5. 天生我材必有用,千金散尽还复来。
  6.     
  
       .com-card {         min-width: 200px;         min-height: 100px;         border-radius: 4px;         border: 1px solid #ebeef5;         background-color: #fff;         overflow: hidden;         color: #303133;         transition: .3s;         box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);     }     .com-card-head {         padding: 10px 20px;         border-bottom: 1px solid #ebeef5;         box-sizing: border-box;     }     .com-card-body {         padding: 20px;     }     .link-wrap {         text-align: left;         padding-top: 20px;     }     .link {         display: inline-block;         height: 42px;         line-height: 43px;         padding: 0 30px;         text-align: center;         cursor: pointer;         color: #fff;         background-color: #409eff;         border-color: #409eff;         -webkit-appearance: none;         box-sizing: border-box;         outline: none;         transition: .1s;         font-weight: 500;         -moz-user-select: none;         -webkit-user-select: none;         -ms-user-select: none;         font-size: 14px;         border-radius: 4px;         text-decoration: none !important;     }      
    
           
    
             
               
    
  
  class ComCard extends HTMLElement {     constructor() {       super();       var shadow = this.attachShadow({mode: 'closed'})  // open       var tplEle = document.getElementById('custom-card-template')       var content = tplEle.content.cloneNode(true)       var attrList = Array.from(this.attributes);       var props = attrList.reduce((prev, item)=>{         prev[item.name] = item.value         return prev       }, {})       if (props['data-show-head']!=='1') {         var head = content.querySelector('.com-card-head')         head.remove()       }       var urlEle = content.querySelector('.link')       if (props['data-url'] && props['data-title']) {         urlEle.href = props['data-url']         urlEle.title = props['data-title']         urlEle.innerText = props['data-title']       } else {         urlEle.remove()       }       shadow.appendChild(content)     }     connectedCallback(){       //在这里发送数据请求(Ajax)       console.log('connectedCallback')     }     //被从文档DOM中删除时调用     disconnectedCallback(){       console.log('disconnectedCallback')     }     //被移动到新的文档时调用     adoptedCallback(){       console.log('adoptedCallback')     }     //当增加、删除、修改自身的属性时被调用     attributeChangedCallback(){       console.log('attributeChangedCallback')     }   }   window.customElements.define('com-card', ComCard);   function hello() {     alert('Hello,Web Component')   }

最终疗效如上图所示

WebComponents 与 VueComponentsVueComponentWebComponent

数据

实例属性

道具

属性

手表

观察到的属性、属性更改回调

计算的

吸气剂

方法

类方法

安装的

连接回调

被摧毁

断开连接回调

风格范围

模板中的样式

模板

模板

WebComponents生命周期反弹函数

connectedCallback:当自定义元素第一次插入文档 DOM 时调用。

disconnectedCallback:当自定义元素从文档 DOM 中删除时调用。

采用回调:当自定义元素连接到新文档时调用。

attributeChangedCallback:当customelement降低、删除或修改自己的属性时调用。

的优点和缺点

优势:

缺点:

7.基于webcomponents的框架

LitElement 是一个快速、轻量级的 WebUI 框架。 使用 lit-html 渲染元素。

Polymer是一个实用的、风暴驱动的、封装的、交互式的WebUI框架。

Omi是一个基于Web组件的跨框架跨平台框架。 中国联通终端&桌面&小程序。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悟空资源网 elementui elementui选择卡片-Web Components如何在Element UI类中实现Card卡片 https://www.wkzy.net/game/147195.html

常见问题

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务