Web component--01. Brief introduction

Source: Internet
Author: User

What is WEB components?

WEB Components is a new standard for the definition of the website, which gives front-end developers the ability to expand their browser tags, freely customize the component, and better modular development, completely liberating the productivity of front-end developers.

WEB Components Architecture

There are several modules in the development of WEB components in the specification:

    • Template elements
    • Html Import
    • Shadow DOM
    • Custom elements
    • Decorative Device

Currently the top four modules are sufficient to support Web Component, and adorners do not yet have a complete specification.

Template element

Create a template HTML tag that gets the templates content of the node through JavaScript

<template id="test">test template</template>

Templates are not displayed by default, you need to activate the template, the following two ways to activate the node

    • Clone node

      var templateContent  = template.content;var activeNode = templateContent.cloneNode(true);document.body.appendChild(activeNode);
    • Import node

      var templateContent  = template.content;var activeNode = document.importNode(templateContent,true);document.body.appendChild(activeNode);
Html Import

HTML Import can embed external HTML documents into the current document, providing good resource sharing.
Link with import attribute supports two events

    • OnLoad: File successfully introduced to page will trigger

    • OnError: File load Failure triggers

      <script type= "Text/javascript" > Function importtest (message) {console.log (message);} </script>

Shadow DOM

Before the Web Component specification came out, the arguments about HTML, CSS, and Javascript for building WEB applications were constantly debated. There are several main questions:

    • Style overrides: The style of the document affects the Web Component
    • Script substitution: The document's JavaScript overwrites some of the code in the Web Component
    • Duplicate ID: Duplicate ID in document causes parsing exception

The introduction of Shadow DOM is to solve the problem of encapsulation mechanism scope.
Browsers typically do not see the shadow DOM node, and Google development tools can help me review these elements, which need to be set up as follows:

Create Shadow DOM: Create a Shadow dom subtree from the Createshadowroot function on a DOM element (host Element)

<div id="box"></div><!--容器--><template id="test"><style>:host h1{color:red};</style>
Custom elements

The development of a custom element requires five steps:

    • To create an object:

      var objectProto = Object.create(HTMLElement.prototype);
    • To define object properties:

      //定义单个属性Object.defineProperty(objectProto,‘title‘,{    writable : true,})//定义单个多个Object.defineProperties(objectProto,{    height: {writable : true},    width: {writable : true}})
    • Define Life cycle methods:

      //成功创建对象objectProto.createdCallback = function(){    console.log(‘created‘);}//对象插入DOM中objectProto.attachedCallback = function(){    console.log(‘attached‘);}
    • Registering new elements

      document.registerElement(‘test‘,{        prototype : objectProto});

Enter the HelloWorld of 24px:

<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>
Clock applications
    • test.html: Load Clock Component by import
    • Clock-elemect.html: Responsible for the countdown implementation

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= "Minu TE ">MM:</div> <div class=" Second ">SS</div> </div></template><script type=" Te    Xt/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>



From for notes (Wiz)



Web component--01. Brief introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.