敦煌 |玉门 |瓜州 |金塔 |甘肃 |互联网 |教育 |社会 |

实时·准确·聚焦

当前位置:社会资讯 > 互联网> Web组件即将取代前端框架?!|技术头条

Web组件即将取代前端框架?!|技术头条

2019-05-05 13:19 | CSDN |

原标题:Web 组件即将取代前端框架?!| 技术头条

以下为译文:

.customElements.define( , MyElement);

{

() {

();

}

connectedCallback() {

}

}

customElements.define( , {...});

...

el = customElements. ( );

myElement = el();

.body.(myElement);

{

get {

[ , ];

}

attributeChangedCallback(attr, oldVal, newVal) {

(attr) {

:

:

}

}

}

-> ->

() {

.container = .shadowRoot.querySelector( );

}

attributeChangedCallback(attr, oldVal, newVal) {

(attr === ) {

( .hasAttribute( ) {

.container.style.background = ;

}

{

.container.style.background = ;

}

}

}

customElements.whenDefined( )

. ( =>{

my-element now defined

})

{

...

doSomething() {

something method

}

}

element = . ( );

element.doSomething();

< name= >

const = document.querySelector( );

.disabled = ;

=>

{

...

disabled(isDisabled) {

(isDisabled) {

.setAttribute( , );

}

{

.removeAttribute( );

}

}

disabled() {

.hasAttribute( );

}

}

{

static observedAttributes() {

[ ];

}

() {

const shadowRoot = .attachShadow({mode: });

shadowRoot.innerHTML = `

<style>

.disabled {

opacity: ;

}

</style>

<div id= ></div>

`;

.container = .shadowRoot( );

}

attributeChangedCallback(attr, oldVal, newVal) {

(attr === ) {

( .disabled) {

.container.classList.add( );

}

{

.container.classList.remove( )

}

}

}

}

shadowRoot = .attachShadow({ : });

shadowRoot.innerHTML = ;

{

: block;

}

) {

: ;

}

{

: initial;

}

{

: inline-block;

}

{

: block;

}

{

: (--background-color);

}

{

: ;

}

{

: ;

}

{

: (--background-color);

}

>

==>

==>

>

=>

=>

=></>

>

>

=>

=>

=>

==>

==>

>

>

>


Web组件即将取代前端框架?!|技术头条

=>

=>

></>

></>

>

>No image here! >>

>

>

>

=>

=>

>

=>

>

>

=>

>

>

>No image here! >

>

>

>

{

: left;

}

>

=>

=>

>

>

slot.addEventListener( , e => {

changedSlot = e.target;

.log(changedSlot.assignedNodes());

});

{

...

connectedCallback() {

.dispatchEvent( CustomEvent( , {

detail: {message: }

}));

}

}

. ( ).addEventListener( , e => console.log( , e.detail.message));

{

...

connectedCallback() {

.container = .shadowRoot.querySelector( );

dispatchEvent now called .container instead

.container.dispatchEvent( CustomEvent( , {

detail: {message: },

composed: without composed: event will bubble out Shadow DOM

}));

}

}

{

...

() {

const shadowRoot = .attachShadow({mode: });

.shadowRoot.innerHTML = `

<template id= >

<p>This view </p>

</template>

<template id= >

<p>This view </p>

</template>

<div id= >

<p>This the container</p>

</div>

`;

}

connectedCallback() {

const content = .shadowRoot.querySelector( ).content.clondeNode( );

.container = .shadowRoot.querySelector( );

.container.(content);

}

}

{

...

constructor() {

();

}

connectedCallback() {

...

}

someMethod() {

...

}

}

customElements.define( , MyButton, { : });

<button = >

>

>

=></>

>

>

;

describe( , =>{

let element;

beforeEach( =>{

element = .( );

.body.(element);

});

afterEach( =>{

.body.removeChild(element);

});

it( , =>{

run your test here

});

});

>

>

=>

==>

=></>

=></>

=></>

=></>

>

.assert = chai.assert;

mocha.setup();

</>

==></>

=>

mocha.run();

</>

>

>

=></>

>

>

install --save @webcomponents/webcomponentsjs

=

=

=

=

=></>

<img

=

src= http://www.sohu.com/a/

width=

height=

delay=

margin= >

<template id= >

<h1>{{title}}</h1>

<p>{{text}}</p>

</template>

template = . ( );

instance = template.createInstance({title: , text: });

shadowRoot.(instance.content);

instance.update({title: , text: });

  • 原生,不需要框架;
  • 易于集成,不需要移位;
  • CSS有真正的作用域;
  • 规范,只有HTML, CSS和Java。

上一篇:
下一篇:
(责任编辑:)
图片推荐
图文资讯