Web Component--01. 简介
Web Components 是什么?
Web Components是W3C定义的新标准,它给了前端开发者扩展浏览器标签的能力,可以自由的定制组件,更好的进行模块化开发,彻底解放了前端开发者的生产力。
Web Components 架构
Web Components在 W3C 规范中的发展有几个模块:
- 模板元素
- Html Import
- Shadow DOM
- 自定义元素
- 装饰器
目前前四个模块足以支撑 Web Component,装饰器还没有一个完整的规范。
template 模板元素
创建一个template的 html 标签,通过 javascript 获取节点的模板内容
<template id="test">
test template
</template>
<h1 id="message"></h1>
<script type="text/javascript">
var template = document.getElementById("test");
console.log(template.content);
</script>
模板默认不显示,需要激活模板,通过以下两种方法来激活节点
克隆节点
var templateContent = template.content;
var activeNode = templateContent.cloneNode(true);
document.body.appendChild(activeNode);
导入节点
var templateContent = template.content;
var activeNode = document.importNode(templateContent,true);
document.body.appendChild(activeNode);
Html Import
Html Import 可以将外部的 HTML 文档嵌入到当前文档中,提供很好的资源共享。
带有import属性的link 支持两个事件
onload:文件成功引入页面会触发
onerror: 文件加载失败会触发
<script type="text/javascript">
function importTest(message){
console.log(message);
}
</script>
Shadow DOM
在 Web Component 规范出来之前,关于 HTML、CSS、Javascript 构建 Web 应用程序的程序的争论一直不断。主要质疑有几种:
- 样式覆盖:文档的样式会影响Web Component
- 脚本替换:文档的Javascript会覆盖Web Component的部分代码
- 重复的 ID:文档出现重复 ID 会导致解析异常
Shadow DOM的引入就是为了解决封装机制作用域的问题。
浏览器通常情况下是看不到Shadow DOM节点的,Google 开发工具可以帮我审查这些元素,需要做如下设定:

创建Shadow DOM : 通过 createShadowRoot 函数对一个 DOM 元素(宿主元素)创建一个 Shadow DOM 子树
<div id="box"></div><!--容器-->
<template id="test">
<style>
:host h1{color:red};
</style>
<h1>Test</h1>
</template>
<script type="text/javascript">
var box = document.getElementById("box");
var shadowRoot = box.createShadowRoot();
var template = document.getElementById("test");
var templateContent = template.content;
var activeNode = document.importNode(templateContent,true);
shadowRoot.appendChild(activeNode);
</script>
自定义元素
开发一个自定义元素需要五个步骤:
创建对象:
var objectProto = Object.create(HTMLElement.prototype);
定义对象属性:
//定义单个属性
Object.defineProperty(objectProto,'title',{
writable : true,
}) //定义单个多个
Object.defineProperties(objectProto,{
height: {writable : true},
width: {writable : true}
})
定义生命周期方法:
//成功创建对象
objectProto.createdCallback = function(){
console.log('created');
}
//对象插入DOM中
objectProto.attachedCallback = function(){
console.log('attached');
}
注册新元素
document.registerElement('test',{
prototype : objectProto
});
输入24px 的 HelloWorld:
<my-name title="HelloWorld" fontsize="2"></my-name>
<script type="text/javascript">
var objectProto = Object.create(HTMLElement.prototype);
Object.defineProperties(objectProto,{
title: {writable : true},
fontsize: {writable : true}
})
objectProto.createdCallback = function(){
this.innerText = this.attributes.title.value;
this.style.fontSize = this.attributes.fontsize.value * 12 + 'px';
}
document.registerElement('my-name',{
prototype : objectProto
});
</script>
时钟应用
- test.html :通过import方式加载Clock Component
- clock-elemect.html:负责倒计时的实现
test.html
<link rel="import" href="clock-element.html"/>
<digital-clock></digital-clock>
clock-elemect.html
<template id="clockTemplete">
<style>
:host::shadow .clock{
display: inline-flex;
justify-content: space-around;
background:white;
font-size: 8rem;
box-shadow: 2px 2px 4px -1px grey;
border: 1px solid green;
font-family: sans-serif;
width: 100%;
}
:host::shadow .clock .hour,
:host::shadow .clock .minute,
:host::shadow .clock .second{
color: orange;
padding: 1.5rem;
text-shadow: 0px 2px black;
}
</style>
<div class="clock">
<div class="hour">HH:</div>
<div class="minute">MM:</div>
<div class="second">SS</div>
</div>
</template>
<script type="text/javascript">
(function(){
var selfDoucment = document.currentScript.ownerDocument;
var objectProto = Object.create(HTMLElement.prototype);
objectProto.createdCallback = function(){
var shadow = this.createShadowRoot();
var templateContent = selfDoucment.querySelector('#clockTemplete').content;
var templateNode = selfDoucment.importNode(templateContent,true);
shadow.appendChild(templateNode);
var hourElement = shadow.querySelector('.hour');
var minuteElement = shadow.querySelector('.minute');
var secondElement = shadow.querySelector('.second');
window.setInterval(function(){
var date = new Date();
hourElement.innerText = date.getHours()+':';
minuteElement.innerText = date.getMinutes()+':';
secondElement.innerText = date.getSeconds();
},1000);
};
document.registerElement('digital-clock',{
prototype : objectProto
});
})();
</script>
Web Component--01. 简介的更多相关文章
- amazeui学习笔记二(进阶开发2)--Web组件简介Web Component
amazeui学习笔记二(进阶开发2)--Web组件简介Web Component 一.总结 1.amaze ui:amaze ui是一个web 组件, 由模板(hbs).样式(LESS).交互(JS ...
- Node.js 教程 01 - 简介、安装及配置
系列目录: Node.js 教程 01 - 简介.安装及配置 Node.js 教程 02 - 经典的Hello World Node.js 教程 03 - 创建HTTP服务器 Node.js 教程 0 ...
- Web Component 文章
周末无意中了解了Web Component的概念. http://blog.amowu.com/2013/06/web-components.html http://www.v2ex.com/t/69 ...
- 示例可重用的web component方式组织angular应用模块
在online web应用中,经常有这样的需求,能够让用户通过浏览器来输入代码,同时能够根据不同的代码来做语法高亮.大家已知有很多相应的javascript库来实现语法高亮的功能,比如codemirr ...
- (转)Web Service入门简介(一个简单的WebService示例)
Web Service入门简介 一.Web Service简介 1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从I ...
- Web Component总结
Web Component 一个Web组件通常由四个部分组成:模板.Shadow DOM.自定义元素与打包,其中Shadow DOM解决了组件在页面中的封装问题 Shadow DOM 有shadow ...
- Salesforce知识整理(一)之Lightning Web Component Tools
目录 LWC知识整理(一) 工具 Salesforce CLI Visual Studio Code(VS Code) Developer Hub(Dev Hub) 开启Dev Hub 相关资料 茶余 ...
- Web Component
前言 Web Component不是新东西,几年前的技术,但是受限于浏览器兼容性,一直没有大规模应用在项目里,直到现在(2018年年末),除IE仍不支持之外,其它主流浏览器都支持Web Compone ...
- Web Service入门简介(一个简单的WebService示例)
Web Service入门简介 一.Web Service简介 1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从I ...
- Web Component探索
概述 各种网站往往需要一些相同的模块,比如日历.调色板等等,这种模块就被称为“组件”(component).Web Component就是网页组件式开发的技术规范. 采用组件进行网站开发,有很多优点. ...
随机推荐
- HTML、XHTML XML和DHTML的区别
XML与HTML的设计区别是:XML是用来存储数据的,重在数据本身.而HTML是用来定义数据的,重在数据的显示模式 XHTML(The Extensible HyperText Markup Lang ...
- solr详解,开发必备
1.基础知识 创建索引的过程如下: (1).建立索引器IndexWriter,这相当于一本书的框架 (2).建立文档对象Document,这相当于一篇文章 (3).建立信息字段对象Field,这相当于 ...
- js 日期有效性验证 的一点思考
在日常项目中经常遇到日期输入验证,以前我遇到的项目是日期只能通过日历控件来选择,最近我同事遇到一个问题是日期除了可以通过日历控件来输入也可以手动来输入,可是我们项目中居然没有日期格式的验证方法(备注: ...
- 解决Visual Studio 2010/2012的RC4011 warnings
如果在vc10/11工程的rc文件中有以下任意一行代码: #include <winuser.h> #include <richedit.h> 那么vc将会给出一对警告: C: ...
- gpg的一些常用操作
(1)列出keys # gpg --list-keys /root/.gnupg/pubring.gpg ------------------------ pub 2048R/98681A63 2 ...
- RabbitMQ的安装使用
1.下载安装 Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装RabbitMQ之前要先安装Erlang. erlang:http://www.erlang.org/downloa ...
- Linux--Ubuntu中文文件夹转英文
前言 在安装Ubuntu的时候,如果选择的系统语言为汉语,安装完成后,Ubuntu系统的主文件夹下的几个文件目录就是中文的,这样才纯终端下,输入起来确实非常的不方便.当然,如果安装Ubuntu的时候, ...
- mybatis 返回null 及 参数说明
'org.mybatis:mybatis:3.2.8' (会与 'org.mybatis:mybatis:3.1.1',com.mybank.tools.dialect.PaginationInter ...
- Android开发(三十一)——重复引用包错误Conversion to Dalvik format failed
错误:Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Landroid/sup ...
- android Camera 结构
Java层 :Frameworks\base\core\java\android\hardware\Camera.java JNI: Frameworks\base\core\jni\android_ ...