Workshop 4SH
Il est possible que je dise de grosses bêtises
import {html, render} from 'lit-html';
// This is a lit-html template function. It returns a lit-html template.
const helloTemplate = (name) => html`<div>Hello ${name}!</div>`;
// This renders <div>Hello Steve!</div> to the document body
render(helloTemplate('Steve'), document.body);
// This updates to <div>Hello Kevin!</div>, but only updates the ${name} part
render(helloTemplate('Kevin'), document.body);A simple base class for creating fast, lightweight web components
import { LitElement, html } from 'lit-element';
class CounterComponent extends LitElement {
static get properties() {
return {
value: {type: Number},
max: {type: Number}
};
}
constructor() {
super();
this.max = Infinity;
this.value = 1;
}
render() {
return html`
<div>
Counter :
<button @click="${this.increment}">+</button>
${this.value}
<button @click="${this.decrement}">-</button>
</div>
`;
}
increment(event) { this.value = Math.min(this.value+1, this.max); }
decrement(event) { this.value--; }
}
customElements.define('hello-counter', CounterComponent)<hello-counter value="10" max="20"></hello-counter>import { LitElement, html, property, customElement } from 'lit-element';
@customElement('hello-counter')
class CounterComponent extends LitElement {
@property({ type: Number }) value = 1;
@property() max = Infinity;
render() {
return html`
<div>
Counter :
<button @click="${this.increment}">+</button>
${this.value}
<button @click="${this.decrement}">-</button>
</div>
`;
}
increment() { this.value = Math.min(this.value+1, this.max); }
decrement() { this.value--; }
}
declare global {
interface HTMLElementTagNameMap {
'hello-counter': CounterComponent
}
}
<hello-counter value="10" max="20"></hello-counter>$> npm install --save-dev viteLe source des live-coding :
Discovering Lit-Element - Workshop 4SH