概述

之前我们介绍了Web Components的基本概念,现在我们给出一个使用Web Components的实例代码,并且对组件化进行一些思考。记录下来,供以后开发时参考,相信对其他人也有用。

实例代码

参考资料:web-components-examples

html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Pop-up info box — web components</title>
</head>
<body>
<h1>Pop-up info widget - web components</h1> <form>
<div>
<label for="cvc">Enter your CVC <popup-info img="img/alt.png" data-text="Your card validation code (CVC) is an extra security feature — it is the last 3 or 4 numbers on the back of your card."></label>
<input type="text" id="cvc">
</div>
</form> <script src="main.js"></script> </body>
</html>

js:

// Create a class for the element
class PopUpInfo extends HTMLElement {
constructor() {
// Always call super first in constructor
super(); // Create a shadow root
const shadow = this.attachShadow({mode: 'open'}); // Create spans
const wrapper = document.createElement('span');
wrapper.setAttribute('class', 'wrapper'); const icon = document.createElement('span');
icon.setAttribute('class', 'icon');
icon.setAttribute('tabindex', 0); const info = document.createElement('span');
info.setAttribute('class', 'info'); // Take attribute content and put it inside the info span
const text = this.getAttribute('data-text');
info.textContent = text; // Insert icon
let imgUrl;
if(this.hasAttribute('img')) {
imgUrl = this.getAttribute('img');
} else {
imgUrl = 'img/default.png';
} const img = document.createElement('img');
img.src = imgUrl;
icon.appendChild(img); // Create some CSS to apply to the shadow dom
const style = document.createElement('style');
console.log(style.isConnected); style.textContent = `
.wrapper {
position: relative;
}
.info {
font-size: 0.8rem;
width: 200px;
display: inline-block;
border: 1px solid black;
padding: 10px;
background: white;
border-radius: 10px;
opacity: 0;
transition: 0.6s all;
position: absolute;
bottom: 20px;
left: 10px;
z-index: 3;
}
img {
width: 1.2rem;
}
.icon:hover + .info, .icon:focus + .info {
opacity: 1;
}
`; // Attach the created elements to the shadow dom
shadow.appendChild(style);
console.log(style.isConnected);
shadow.appendChild(wrapper);
wrapper.appendChild(icon);
wrapper.appendChild(info);
}
} // Define the new element
customElements.define('popup-info', PopUpInfo);

组件化思考

在MV*架构出现之前,组件主要分为两种:一种是狭义上的组件,比如UI组件;另一种是广义上的组件,即带有业务含义和数据的UI组件组合。

对于UI组件,一定有这3个部分:结构、样式和交互行为,分别对应HTML,CSS和Js。

在js中,我们利用class对组件进行封装,然后在使用的时候,我们只需实例化一个组件对象然后传入必要的参数即可。

对于组件的js结构,有以下3个标准:

  1. 基本的封装性。我们利用class进行封装。
  2. 简单的生命周期呈现。比如constructor和destroy
  3. 明确的数据流动。这里的数据指的参数,一旦确定参数的值,就能够进行渲染。

上面的组件化会遇到一个问题,就是js里面存在大量的show,hide和toggle等方法,当逻辑一旦复杂,就会出现大量的DOM操作,严重影响性能,也加大了维护的难度。

这个时候就出现了模板引擎,它使我们在js里面拼装html代码,然后一起插入到html里面去,并且把数据独立出来,也解决了数据与界面耦合的问题。

再然后就是Web Components和MV*的组件化,他们开辟了新的组件建立方式~

Web Components(续)的更多相关文章

  1. 【shadow dom入UI】web components思想如何应用于实际项目

    回顾 经过昨天的优化处理([前端优化之拆分CSS]前端三剑客的分分合合),我们在UI一块做了几个关键动作: ① CSS入UI ② CSS作为组件的一个节点而存在,并且会被“格式化”,即选择器带id前缀 ...

  2. Web Components初探

    本文来自 mweb.baidu.com 做最好的无线WEB研发团队 是随着 Web 应用不断丰富,过度分离的设计也会带来可重用性上的问题.于是各家显神通,各种 UI 组件工具库层出不穷,煞有八仙过海之 ...

  3. Web Components之Custom Elements

    什么是Web Component? Web Components 包含了多种不同的技术.你可以把Web Components当做是用一系列的Web技术创建的.可重用的用户界面组件的统称.Web Com ...

  4. 【转】Facebook React 和 Web Components(Polymer)对比优势和劣势

    原文转自:http://segmentfault.com/blog/nightire/1190000000753400 译者前言 这是一篇来自 StackOverflow 的问答,提问的人认为 Rea ...

  5. 可选的Web Components类库

    首先需要说明的是这不是一篇 Web Components 的科普文章,如果对此了解不多推荐先读<A Guide to Web Components>. 有句古话-“授人以鱼,不如授人以渔” ...

  6. Polymer——Web Components的未来

    什么是polymer? polymer由谷歌的Palm webOS团队打造,并在2013 Google I/O大会上推出,旨在实现Web Components,用最少的代码,解除框架间的限制的UI 框 ...

  7. The state of Web Components

    Web Components have been on developers’ radars for quite some time now. They were first introduced b ...

  8. Web Components

    Web Components是不是Web的未来   今天 ,Web 组件已经从本质上改变了HTML.初次接触时,它看起来像一个全新的技术.Web组件最初的目的是使开发人员拥有扩展浏览器标签的能力,可以 ...

  9. 前端应该知道的Web Components

    前端组件化的痛点 在前端组件化横行的今天,确实极大的提升了开发效率.不过有一个问题不得不被重视,拟引入的这些html.css.js代码有可能对你的其他代码造成影响. 虽然我们可以通过命名空间.闭包等一 ...

随机推荐

  1. mysql备份最近8天的数据库,老的自动删除方案

    服务器上的处理脚本记录: [root@mysql01 test]# crontab -l0 2 * * * /bin/sh /script/sqlbackup.sh >/dev/null 2&g ...

  2. linux学习第十六天 (Linux就该这么学)

    今生讲了邮件的产生和解决和实际问题,把前两天的和这节邮箱系统统一布置,又统一复习和学习了一下,

  3. GUI学习之八——QToolButton的学习总结

    QToolButton提供一个快速的访问按钮,通常在工具栏内使用,一般不显示文本标签而显示图标. 一.按钮的样式风格设置 可以按照下面的风格对按钮进行样式设置 从左到右依次是仅显示图标.仅显示文字.图 ...

  4. 代码简洁的滑动门(tab)jquery插件

    < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org ...

  5. c语言实验一

    #include <stdio.h> int main(){ int a,b,sum; a=123; b=456; sum = a + b; printf("sum is %d\ ...

  6. python networkx:绘制网络图

    1.简单使用 import networkx as nx import matplotlib.pyplot as plt G = nx.Graph() G.add_edge(1,2) nx.draw_ ...

  7. Mysql中存储引擎区别【 InnoDB、MyISAM】

    区别: 1. InnoDB支持事务,MyISAM不支持,对于InnoDB每一条SQL语言都默认封装成事务,自动提交,这样会影响速度,所以最好把多条SQL语言放在begin和commit之间,组成一个事 ...

  8. Startls Back 引起的 win10升级之后的闪屏问题

    win10 更新之后出现闪频问题. 有人说是和startls back 有关,需要卸载startls back, 但是进入安全模式下显示此 程序无法打开,无法卸载. 后来看到有人更新到startls ...

  9. cpu的工作原理

  10. 实现highcharts放大缩小

    原文地址:http://www.stepday.com/topic/?800 当我们将图表某个区域放大值某一个倍数后发现刻度间隔距离也放大了,由于刻度间隔还是原来初始所设定的值,从而让局部数据的X轴刻 ...