Google Lit Web Components library All In One
Google Lit Web Components library All In One
Lit is a simple library for building fast, lightweight web components.
Lit 是一个简单的库,用于构建快速、轻量级的 Web 组件。
lit
https://lit.dev/docs/getting-started/
https://www.youtube.com/channel/UCok4ZKSzM3jY7JQRMlF-DPg
demo
import {html, css, LitElement} from 'lit';
export class SimpleGreeting extends LitElement {
static styles = css`p { color: blue }`;
static properties = {
name: {type: String},
};
constructor() {
super();
this.name = 'Somebody';
}
render() {
const result = html`<p>Hello, ${this.name}!</p>`;
console.log('result =', result);
// {strings, values, _$litType$}
return result;
}
}
customElements.define('simple-greeting', SimpleGreeting);
<!DOCTYPE html>
<head>
<script type="module" src="./simple-greeting.js"></script>
</head>
<body>
<simple-greeting name="World"></simple-greeting>
</body>
TypeScript & @decorators
TypeScript 修饰器实现原理 / TypeScript @decorator under the hood
import {html, css, LitElement} from 'lit';
import {customElement, property} from 'lit/decorators.js';
@customElement('simple-greeting')
export class SimpleGreeting extends LitElement {
static styles = css`p { color: blue }`;
@property()
name = 'Somebody';
render() {
return html`<p>Hello, ${this.name}!</p>`;
}
}
import {html, css, LitElement} from 'lit';
export class SimpleGreeting extends LitElement {
static styles = css`p { color: blue }`;
static properties = {
name: {type: String},
};
constructor() {
super();
this.name = 'Somebody';
}
render() {
return html`<p>Hello, ${this.name}!</p>`;
}
}
customElements.define('simple-greeting', SimpleGreeting);
Web Components
-
Custom elements
A set of JavaScript APIs that allow you to define custom elements and their behavior, which can then be used as desired in your user interface. -
Shadow DOM
A set of JavaScript APIs for attaching an encapsulated "shadow" DOM tree to an element — which is rendered separately from the main document DOM — and controlling associated functionality.
In this way, you can keep an element's features private, so they can be scripted and styled without the fear of collision with other parts of the document. -
HTML templates
The<template>
and<slot>
elements enable you to write markup templates that are not displayed in the rendered page.
These can then be reused multiple times as the basis of a custom element's structure.
refs
https://developer.mozilla.org/en-US/docs/Web/Web_Components
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16026041.html
未经授权禁止转载,违者必究!