比较制作Web组件的33种不同方式的编码风格、包大小和性能。
尝试使用WebComponents.dev类MyCounter扩展HTMLElement{structor(){Super();this.count=0;const style=`*{font-size:200%;}span{width:4rem;display:inline-block;text-align:center;}button{width:64px;Height:64px;edge:None;edge-Radius:10px;Background-color:Segreen;color:White;}`;const html=`&l。Span>;${this.count}<;/span>;<;按钮id=";Inc";>;+<;/button>;`;this.attachShadow({mode:';open&39;});this.shadowRoot.innerHTML=`<;style>;${style}<;/style>;${html}`;This.buttonDec=this.shadowRoot.getElementById(';dec';);this.spanValue=this.shadowRoot.querySelector(';span';);this.inc=this.inc.bind(This);this.dec=this.dec.bind(This);}Inc(){this.count++;this.update();}dec(){this.count--;this.update();}UPDATE(){this.spanValue.innerText=this.count;}connectedCallback(){this.buttonInc.addEventListener(';click';,this.inc);this.buttonDec.addEventListener(';click';,this.dec);}disconnectedCallback(){this.buttonInc.removeEventListener(';click';,this.inc);this.buttonDec.removeEventListener(';click';,this.dec);}}customElements.Define(';My-Counter';,MyCounter);
尝试使用WebComponents.dev import{html,ender}from';lightterhtml';;类MyCounter扩展HTMLElement{structor(){Super();this.count=0;this.attachShadow({mode:';open';});this.update();}Inc=()=>;{this.count++;this.update();};dec=()=>。Style(){return`*{font-size:200%;}span{width:4rem;display:inline-block;text-align:center;}button{width:64px;Height:64px;edge:None;Borde-Radius:10px;Background-color:Segreen;color:White;}`;}template(){return html`<;style>;${this.style()}<;/style>;<。-<;/Button&>;<;span&>;${this.count}<;/SPAN&><;Button onclick=";${this.inc}";>;+<;/button>;`;}UPDATE(){Render(this.shadowRoot,this.template());}}customElements.Define(';My-Counter';,MyCounter);
尝试使用WebComponents.dev import{html,ender}from';lighthtml';;class MyCounter扩展HTMLElement{structor(){Super();this.count=0;this.attachShadow({mode:';open';});this.update();}Inc(){this.count++;this.update();}dec(){this.count--;this.update。}span{宽度:4rem;显示:行内块;文本对齐:居中;}按钮{宽度:64px;高度:64px;边框:无;边框半径:10px;背景颜色:深绿色;颜色:白色;}`;}模板(){Return html`<;style>;${this.style()}<;/style>;按钮@click=";${this.dec}";>;-<;/button>;<;SPAN>;${this.count}<;/span>;<;Button@click=";${this.inc}";>;+<;/button>;`;}UPDATE(){Render(this.Template(),this.shadowRoot,{eventContext:this});}}customElements.Define(';my-Counter';,MyCounter);
Micro html是一个大约2.5K的lightterhtml子集,用于通过模板文字标记构建声明性和反应性UI。
从";uhtml";;类MyCounter扩展HTMLElement{structor(){Super();this.count=0;this.attachShadow({mode:";open";});this.update();}inc=()=>;{this.count++;this.update();};dec=()=>;尝试使用WebComponents.dev import{html,ender};class MyCounter扩展HTMLElement{structor(){Super();this.count=0;this.attachShadow({mode:";open";});Style>;*{FONT-SIZE:200%;}SPAN{Width:4rem;Display:Inline-Block;Text-Align:Center;}按钮{Width:64px;Height:64px;Borde:None;Borde-Radius:10px;Background-Color:Segreen;color:White;}<;/Style&>lt;Button onclick=";${this.dec}";>;-<;/button>;<;SPAN>;${this.count}<;/SPAN>;<;Button onclick=";${this.inc}";>;+<;/button>;`;}UPDATE(){Render(this.shadowRoot,this.Template());}}customElements.Define(";my-Counter";,MyCounter);
尝试使用WebComponents.dev import{StacheElement}from";CAN";;//扩展组件以定义自定义元素类MyCounter扩展StacheElement{structor(){Super();this.viewRoot=this.attachShadow({mode:";open";});}static view=`<;style>;*{font-size:200%;}span{width:4rem;背景颜色:深绿色;颜色:白色;}<;/Style>;<;Button on:click=";this.decrement()";>;-<;/button>;<;span>;{{this.count}}<;/span>;<;Button on:click=";this.increment()";>;+<;/button>;`;静态道具={计数:0};增量(){this.count++;}deducment(){this.count--;}}customElements.Define(";my-count";,MyCounter)
尝试使用WebComponents.dev从";Hyperhtml-Element";;类MyCounter扩展HyperHTMLElement{structor(){Super();this.attachShadow({mode:";open";});}Created(){this.count=0;this.ender();}Inc=()=>;{this.count++;this.ender();};dec。Render(){return this.html`<;style>;*{font-size:200%;}span{width:4rem;display:inline-block;text-align:center;}button{width:64px;Height:64px;edge:None;Edge-Radius:10px;Background-color:Segreen;color:White;}<;/style>;button onclick=${this.dec}>;按钮onclick=${this.dec}>。/span>;<;button onclick=${this.inc}>;+<;/button>;`;}}MyCounter.Define(";My-Counter";);
尝试使用WebComponents.dev import{LitElement,html,css}from';Lit-Element';导出类MyCounter扩展LitElement{静态属性={count:{type:number}};静态样式=css`*{font-size:200%;}span{width:4rem;display:inline-block;text-align:center;}按钮{width:64px;Height:64px;Borde:None;Borde-Radius:10px。}Inc(){this.count++;}decc(){this.count--;}Render(){Return@click=";${this.dec}";>;-<;/button>;html`<;Button@click=";${this.inc}";>;+<;/button>;<;${this.count}<;/SPAN&><;Button html`;}}customElements.Define(';My-Counter';,MyCounter);
尝试使用WebComponents.dev import{ComponentMixin}from";@Neow/core";;class MyComponent扩展ComponentMixin(HTMLElement){static template=`<;style>;*{font-size:200%;}span{width:4rem;display:inline-block;text-align:center;}按钮{width:64px;Height:64px;edge:None;edge-Radius:10px。{{this.dec()}}";>;-<;/BUTTON>;<;{{this.counter}}<;/SPAN>;<;Button onclick=";{{this.inc()}}";>;+<;/button>;`;Counter=0;Inc(){this.count++;}dec(){this.count--;}}customElements.Define(";My-Counter";,MyComponent);
前端跨框架框架-将Web组件、JSX、虚拟DOM、函数风格、观察或代理合并到一个小尺寸和高性能的框架中。只需编写一次组件,即可在任何地方使用,如Omi、Reaction、Preact、Vue或ANGING。
尝试使用WebComponents.dev import{Define,WeElement,html}from";OMI";;class MyCounter扩展WeElement{static get proTypes(){return{count:number};}static get defaultProps(){return{count:0};}install(){this.data={count:this.pros.count};}static get CSS(){return`*{font-size:200%;}span{width:4width。高度:64px;边框:无;边框半径:10px;背景颜色:淡褐色;颜色:白色;}`;}inc=()=>;{this.data.count++;this.update();};dec=()=>;{this.data.count--;this.update();};Render(道具){Return html`<;按钮onclick=";${this.dec}";>;-<;/button>;<;SPAN>;${this.data.count}<;/span>;<;Button onclick=";${this.inc}";>;+<;/button>;`;}}定义(";My-Counter";,MyCounter);
尝试使用WebComponents.dev从";@skatejs/element";导入{Render,html}从";litt-html";;class MyCounterElement扩展元素{static get props(){return{count:number};}inc=()=>;{this.count++;};dec=()=>;{this.count--;};Render(){const style=`*{。Text-Align:Center;}按钮{width:64px;Height:64px;Borde:None;Borde-Radius:10px;Background-color:Segreen;color:White;}`;return html`<;style>;${style}<;/style>;<;button@click=";${this.dec}";>;-<;/button>;<;span。${this.inc}";>;+<;/button>;`;}renender(){return Render(this.ender(),this.renderRoot);}}customElements.Define(";My-Counter";,MyCounterElement);
在WebComponents.dev/**@JSX h**/import element,{h}from";@skatejs/element-preact";;class MyCounterElement扩展元素{static get props(){return{count:number};}inc=()=>;{this.count++;};dec=()=>;{this.count--;};Render(){const style=`host*{。Text-Align:Center;}按钮{width:64px;Height:64px;Borde:None;Borde-Radius:10px;Background-color:Segreen;color:White;}`;Return(<;host>;<;style>;/style>;/style>;<;button onclick={this.dec}>;-<;/button>;<;span>;+<;/button>;<;/host>;);}}customElements.Define(";My-Counter";,MyCounterElement);
Slim.js是一个闪电般的快速librr
尝试使用WebComponents.dev从";slm-js/slim.js&34;导入{Tag,Template,useShadow}从";slim-js/Decorators";;@tag(";my-counter";)@useShadow(true)@template(`<;style>;*{font-size:200%;}span{width:4rem;display:inline-block;text-align:center;}按钮{width:64px;Height:64px;edge:None;边框半径:10px;背景颜色:浅绿色;颜色:白色;}<;/style>;<;Button Click=";dec";>;-<;/Button>;<;span>;{{parseCount(count)}}<;/span>;<;Button click=";inc";>;+<;/button>;`)class MyCounter扩展超薄{构造函数(){SUPER();this.count=0;}parseCount(Num){return string(Num);}inc(){this.count++;}dec(){this.count--;}}。
Atomico是一个受Reaction Hooks启发的微库(3.9kB),设计和优化的目的是创建小型、强大、声明性的Web组件,并且只使用函数。
在WebComponents.dev/**@JSX h*/import{h,customElement,useProp}from";Atomico";;function MyCounter(){let[count,setCount]=useProp(";count";);const style=`*{font-size:200%;}span{width:4rem;display:inline-block;text-align:center;}按钮{width:64px;Return(<;host shadowDom&>;<;style&>;{style}<;/style>;<;button onclick={()=>;setCount(count-1)}>;-<;/button>;<;span>;{count}<;/span&>;<;button onclick={()=>;setCount。}我的计数器.props={计数:{类型:数量,反映:真,值:0}};customElements.define(";my-counter";,自定义元素(我的计数器));
尝试使用WebComponents.dev import{component,html,useState,useStyle,css}从";@gallop/gallop";;export const MyCounter=component(";my-counter";,()=>;{const[state]=useState({count:0});useStyle(()=>;css`*{font-size:200%;}span{width:4rem;display。边框-半径:10px;背景颜色:浅绿色;颜色:白色;}`);return html`<;button@click=";${()=>;state.count--}";>;-<;/button>;<;span>;${state.count}<;/span>;<;button@click=";${()=>。},{proList:[]});
尝试使用WebComponents.dev import{html}from";litt-html";;import{component,useState}from";haunted";;function count(){const[count,setCount]=useState(0);return html`<;style>;*{font-size:200%;}span{width:4rem;display:inline-block;text-align:center;}按钮{width:64px。}<;/style&>;<;BUTTON@CLICK=${()=>;setCount(COUNT-1)}>;-<;/BUTTON&>;/BUTTON&>;${COUNT}<;/SPAN&>;BUTTON@CLICK=${()=>;SetCount(COUNT+1)}>;+<;/BUTTON&gT;`;}customElements.define(";my-counter";,组件(计数器。
不要模拟DOM。做DOM吧。通过V1API内置扩展的类似Reaction的自定义元素。
尝试使用WebComponents.dev import{Define}from";heresy";;Define(";MyCounter";,{style:MyCounter=>;`${MyCounter}*{font-size:200%;}${MyCounter}span{width:4rem;display:inline-block;text-align:center;}${MyCounter}按钮{width:64px;Height:64px;edge:None;}`,Render({useState}){const[count,update]=useState(0);this.html`<;button onclick=";${()=>;update(count-1)}";>;-<;/button>;${count}<;/span&>;<;button onclick=";${()=&。}});
不要模拟DOM。做DOM吧。通过V1API内置扩展的类似Reaction的自定义元素。
尝试使用WebComponents.dev import{Define}from";heresy";;Define(";MyCounter";,{style:MyCounter=>;`${MyCounter}*{font-size:200%;}${MyCounter}span{width:4rem;display:inline-block;text-align:center;}${MyCounter}按钮{width:64px;Height:64px;edge:None;},onclick({currentTarget}){this[currentTarget.datet.op]();this.ender();},Inc(){this.count++;},dec(){this.count--;},Render(){this.html`<;button data-op=";dec";onclick=";${this}";>;-<;/button&。按钮数据-op=";Inc";onclick=";${this}";>;+<;/button>;`;}});
Mixds是一个UI库,用于创建基于纯对象和纯函数的具有强大声明和函数方法的Web组件。
尝试使用WebComponents.dev import{html,Define}Function Inc(Host){host.count++;}function dec(Host){host.count--;}const MyCounter={count:0,Render:({count})=>;html`<;style>;*{font-size:200%;}span{width:4rem;display:inline-block;text-align:center。背景颜色:Segreen;color:White;}<;/style>;<;按钮onclick=";${dec}";<;-<;/button>;<;span>;${count}<;/span>;<;button onclick=";${inc}";>;+<;/button>;`};
打包的非常小(3KB)的视图库:Web组件、自定义元素、模板文字、反应式、数据绑定、单向数据流、双向数据绑定、事件处理、道具、生命周期、状态管理、计算属性、指令。没有依赖,没有虚拟DOM,没有构建工具。哇!……但它只是一个观赏图书馆!
尝试使用WebComponents.dev import Litedom from";litedom";;Litedom({tagName:";my-count&34;,shadowDOM:true,template:`<;button@click=";dec";>;-<;/button>;<;span>;{this.count}<;/span>;<;*{font-size:200%;}span{width:4rem;display:inline-block;text-align:center;}按钮{width:64px;Height:64px;Borde:None;Borde-Radius:10px;Background-color:Segreen;color:White;}<;/style>;`,data:{count:0},dec(){this.data.count--;},inc(){this.data.count++;
微型、快速和声明式用户界面开发。使用本地自定义元素API(但不仅仅是)。就这么简单。
尝试使用WebComponents.dev import{component}from";Ottavino";;Component({tag:";my-counter";,shadow:true,template:`<;style>;*{font-size:200%;}span{width:4rem;display:inline-block;text-align:center;}按钮{width:64px;high:64px;edge:None;edge-Radius:10px;Background-。BUTTON onclick=";{{this.decrease()}}";>;-<;/button>;<;span>;{{this.count}}<;/SPAN&>;<;Button onClick=";{{this.increase()}}";>;+<;/button>;`,属性:{count:0},this:{Increate:Function(){this.count++;},Reduce:Function(){this.count--;});
尝试使用WebComponents.dev import{Define}from";Swiss";import{html,ender}from";litt-html";;const counter=(CE)=>;(El)=>;{el.attachShadow({mode:";open";});return{update:()=>;ender(html`<;style>;*{font-size:200%)];返回{update:()=>;Render(html`<;style>;*{font-size:200%。}BUTTON{width:4rem;Height:4rem;Borde:None;Borde-RADIUS:10px;Background-color:Segreen;color:White;}<;/style>;<;Button@click=";${()=>;el.count--}&34;>;-<;/button<;<;span>;${el.count}<;/span>;/span>;${el.count}<;/span>;${el.count}<;/span>;El.count++}";>;+<;/button>;`,el.shadowRoot),};};定义(";my-count";,{props:{count:0},setup:count,});
尝试使用WebComponents.dev从";uce";;定义(";My-Counter";,{attachShadow:{mode:";open";},init(){this.count=0;this.dec=()=>;{this.count--;this.ender();};this.inc=()=>;{this.count++]导入{Define}from";uce";,{attachShadow:{mode:";open";},{attachShadow:{mode:";open";},init()。},Render(){this.html`<;Style>;*{Font-Size:200%;}SPAN{Width:4rem;Display:Inline-Block;Text-Align:Center;}按钮{Width:64px;Height:64px;Borde:None;Borde-Radius:10px;Background-Color:Segreen;color:White;}<;/Style>;Button onclick=";${this.dec}";>;-<;/button>;<;SPAN>;${this.count}<;/SPAN>;<;Button onclick=";${this.inc}";>;+<;/button>;`;}});
尝试使用WebComponents.dev import{LightningElement,api,buildCustomElementConstructor}from";lwc";export默认类MyCounter扩展LightningElement{count=0;Inc(){this.count++;}dec(){this.count--;}}customElements.Define(";my-count";,buildCustomElementConstructor(MyCounter))。
在WebComponents.dev import{element,css,createSignal}from";@lume/element";;export类MyCounter扩展元素{structor(){Super();this._count=createSignal(0);}get count(){return this._count[0]();}set count(Val){this._count[1](Val);}template(){return(<;>;&。(this.count-=1)}>;-<;/button>;<;span>;{this.count}<;/span>;<;button onclick={()=>;(this.count+=1)}>;+<;/button>;<;/>;);}static Get CSS(){return css`*{font-size:200。}BUTTON{width:4rem;Height:4rem;Borde:None;Borde-Radius:10px;Background-color:Segreen;color:White;}`;}}customElements.Define(";My-Counter";,MyCounter);
在WebCompo中试用。
.