tags: 实践经验 summary: Web Components旨在实现HTML、样式和逻辑的复用,相较于Vue、React等框架,Web Components的优点在于无关框架,原生支持。 Created time: December 24, 2022 8:17 PM emoji: https://s1.ax1x.com/2023/01/19/pS8DpIH.png
概述
Web component
Web Components 是一套不同的技术,允许您创建可重用的定制元素(它们的功能封装在您的代码之外)并且在您的web应用中使用它们。
HTML templates(HTML模板):[<template>](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/template) 和 [<slot>](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/slot) 元素使您可以编写不在呈现页面中显示的标记模板。然后它们可以作为自定义元素结构的基础被多次重用。
exportinterfaceComponentOptions {/** * Tag name of the web component. Ideally, the tag name must be globally unique, * so it's recommended to choose a unique prefix for all your components within the same collection. * * In addition, tag name must contain a '-' */ tag:string;/** * If `true`, the component will use scoped stylesheets. Similar to shadow-dom, * but without native isolation. Defaults to `false`. */ scoped?:boolean;/** * If `true`, the component will use native shadow-dom encapsulation, it will fallback to `scoped` if the browser * does not support shadow-dom natively. Defaults to `false`. * * If an object literal containing `delegatesFocus` is provided, the component will use native shadow-dom * encapsulation. When `delegatesFocus` is set to `true`, the component will have `delegatesFocus: true` added to its * shadow DOM. When `delegatesFocus` is `true` and a non-focusable part of the component is clicked: * - the first focusable part of the component is given focus * - the component receives any available `focus` styling * Setting `delegatesFocus` to `false` will not add the `delegatesFocus` property to the shadow DOM and therefore * will have the focusing behavior described for `shadow: true`. */ shadow?:boolean| { delegatesFocus:boolean };/** * Relative URL to some external stylesheet file. It should be a `.css` file unless some * external plugin is installed like `@stencil/sass`. */ styleUrl?:string;/** * Similar as `styleUrl` but allows to specify different stylesheets for different modes. */ styleUrls?:string[] |d.ModeStyles;/** * String that contains inlined CSS instead of using an external stylesheet. * The performance characteristics of this feature are the same as using an external stylesheet. * * Notice, you can't use sass, or less, only `css` is allowed using `styles`, use `styleUrl` if you need more advanced features. */ styles?:string;/** * Array of relative links to folders of assets required by the component. */ assetsDirs?:string[];/** * @deprecated Use `assetsDirs` instead */ assetsDir?:string;}
@Component({ tag:'todo-list',})exportclassTodoList {// Second, we decorate a class member with @Prop() @Prop() name:string;render() {// Within the component's class, its props are// accessed via `this`. This allows us to render// the value passed to `todo-list`return <div>To-Do List Name: {this.name}</div> }}
exportinterfaceEventOptions {/** * A string custom event name to override the default. */ eventName?:string;/** * A Boolean indicating whether the event bubbles up through the DOM or not. */ bubbles?:boolean;/** * A Boolean indicating whether the event is cancelable. */ cancelable?:boolean;/** * A Boolean value indicating whether or not the event can bubble across the boundary between the shadow DOM and the regular DOM. */ composed?:boolean;}
exportclassLoadingIndicator { @Prop() activated:boolean; @State() busy:boolean; @Watch('activated')watchPropHandler(newValue:boolean, oldValue:boolean) {console.log('The new value of activated is: ', newValue); } @Watch('busy')watchStateHandler(newValue:boolean, oldValue:boolean) {console.log('The new value of busy is: ', newValue); }}
组件生命周期
框架集成
web component与框架无关,被原生HTML所支持,因此基本上Vuejs、Reactjs等框架都能使用,此外也可以直接用于原生HTML中。