香草卷筒纸组件

news/2024/7/10 1:36:29 标签: python, java, vue

什么是Web组件? (What are Web Components?)

Web components are custom, reusable web elements which encapsulate functionality, markup structure and styling by using vanilla javascript/HTML/CSS along with native Web APIs.

Web组件是可重用的自定义Web元素,它们通过使用原始javascript / HTML / CSS以及本机Web API来封装功能,标记结构和样式。

Custom elements teach the browser new tricks while preserving the benefits of HTML

自定义元素在保持HTML优势的同时,教给浏览器新技巧

Eric Bidelman

埃里克·比德尔曼 ( Eric Bidelman)

They can be used in the template like common HTML elements (e.g.<p> or <span> ).

它们可以像通用HTML元素(例如<p><span> )一样在模板中使用。

<body>
<p>a simple paragraph</p> <script type="module" src="my-custom-element.js"></script>
<my-custom-element></my-custom-element>
</body>

They build upon ECMAScript 2015 class syntax by extending the HTMLElement interface, that ships useful APIs for manipulating the element or responding to events.

它们通过扩展HTMLElement接口建立在ECMAScript 2015类语法的基础上,该接口提供了用于操纵元素或响应事件的有用API 。

class MyCustomElement extends HTMLElement {}

You can also extend existing HTML elements like HTMLInputElement, HTMLButtonElement, or HTMLParagraphElement to inherit their properties and methods. Thereby you’re able to create your own implementations of common HTML elements.

您还可以扩展现有HTML元素(如HTMLInputElementHTMLButtonElementHTMLParagraphElement以继承其属性和方法。 因此,您可以创建自己的通用HTML元素实现。

生命周期挂钩 (Life Cycle Hooks)

There are a few predefined callback functions you can use for reacting to specific life cycle events, like

您可以使用一些预定义的回调函数来响应特定的生命周期事件,例如

  • connectedCallback — this callback is called whenever your element is inserted into the DOM

    connectedCallback每当您的元素插入DOM时,都会调用此回调

  • disconnectedCallback — is called whenever the element is removed from the DOM tree

    disconnectedCallback每当从DOM树中删除元素时调用

These callbacks are useful for safely fetching/accessing data or running some clean up code.

这些回调对于安全地获取/访问数据或运行一些清理代码很有用。

影子DOM (Shadow DOM)

The encapsulation of the styling and markup structure is achieved by attaching a so called Shadow DOM to your web component, which creates a hidden DOM tree beneath your custom HTML element.

样式和标记结构的封装是通过将所谓的Shadow DOM附加到您的Web组件来实现的,该组件在您的自定义HTML元素下创建了一个隐藏的DOM树。

Shadow DOM in rendered HTML
Shadow DOM visible Chrome DevTools elements tab
Shadow DOM可见的Chrome DevTools元素选项卡

The Shadow DOM is useful when the components markup and style should remain unaffected by global style definitions.

当组件标记和样式应不受全局样式定义的影响时,Shadow DOM很有用。

浏览器支持 (Browser Support)

Web components are supported by all modern browsers like Chrome, Firefox and Opera (for Edge, Safari and IE11 you can use polyfills).

所有现代浏览器(例如Chrome,Firefox和Opera)都支持Web组件(对于Edge,Safari和IE11,您可以使用polyfills )。

构架 (Frameworks)

There are a few frameworks, which will accelerate your web component development, like Polymer or Hybrids. Before adding the framework of your choice to your project try to evaluate whether you’ll really need this additional dependency.

有一些框架可以加速您的Web组件开发,例如PolymerHybrids 。 在将选择的框架添加到项目中之前,请尝试评估您是否真的需要此附加依赖项。

In case you want to create just a few simple components and not an entire component library, you should take the no-framework-path.

如果只想创建几个简单的组件而不是整个组件库,则应采用no-framework-path。

实施Web组件 (Implementing a Web Component)

After clarifying all the necessary fundamentals let’s get our hands dirty and implement a vanilla web component. The goal is to create a basic web component, which contains encapsulated styles/markup and which can be easily extended.

弄清所有必要的基础知识之后,让我们动手做一个简单的Web组件。 目标是创建一个基本的Web组件,其中包含封装的样式/标记,并且可以轻松地对其进行扩展。

At the end of this article you’ll also find a link to my simple web component template repository.

在本文的结尾,您还将找到一个指向我的简单Web组件模板存储库的链接。

建立 (Setup)

For the purpose of demonstration we’ll create a simple web page where our custom web element will be used.

为了进行演示,我们将创建一个简单的网页,其中将使用我们的自定义Web元素。

To check whether our custom element works as expected, I’ll use the tool serve to host our simple web page.

要检查我们的自定义元素是否按预期工作,我将使用该工具用来承载我们简单的网页。

With serve you’ll just have to run the serve . command in the directory where your simple web page resides to boot up a web server, that hosts it on localhost:5000.

使用发球区,您只需运行serve . 简单网页所在目录中的命令来启动Web服务器,该服务器将其托管在localhost:5000

基金会 (The Foundation)

First we’ll extend the HMTMLElement interface to get the required APIs and to register our class as a custom element, so we can use it in our example HTML page.

首先,我们将扩展HMTMLElement接口以获取所需的API并将类注册为自定义元素,因此可以在示例HTML页面中使用它。

Now you can already add it to the web page by importing the javascript file and by inserting the html tag, which we’ve defined in the customElements.define()call as the first argument.

现在,您已经可以通过导入javascript文件并插入html标签(将我们在customElements.define()调用中定义为第一个参数customElements.define()将其添加到网页中。

Do not forget to import the javascript file as type="module" otherwise it won’t get loaded.

不要忘记将javascript文件导入为type="module"否则将不会被加载。

Now we’ve already fulfilled the required steps for utilizing our own web component. Of course, our custom element doesn’t have any content yet but we’re going to change that in the next section.

现在,我们已经完成了利用我们自己的Web组件的必要步骤。 当然,我们的custom元素还没有任何内容,但是我们将在下一部分中对其进行更改。

With this solid foundation we’ll continue and add encapsulated markup structure and styles to our custom element by using the Shadow DOM APIs.

在这个坚实的基础上,我们将继续使用Shadow DOM API将封装的标记结构和样式添加到我们的自定义元素中。

使用影子DOM (Using the Shadow DOM)

As already mentioned the Shadow DOM is a hidden DOM tree, which is attached to your custom element. This ensures that the styles of our custom element aren’t affected by global or another elements styles.

如前所述,Shadow DOM是一个隐藏的DOM树,它附加到您的自定义元素上。 这样可以确保我们的自定义元素的样式不受全局或其他元素样式的影响。

Let’s attach a Shadow DOM to our custom element. We’ll do that in the constructor and choose open as our desired encapsulation mode.

让我们将Shadow DOM附加到我们的自定义元素上。 我们将在constructor执行此操作,然后选择open作为所需的封装模式。

When setting the encapsulation mode to open we’re making sure that our Shadow DOM child elements are accessible via javascript.

将封装模式设置为open我们确保可以通过JavaScript访问Shadow DOM子元素。

Next we should add a template and some styles to our custom element.

接下来,我们应该向我们的自定义元素添加一个模板和一些样式。

Notice that our markup structure resides in a<template> HTML element, which won’t get displayed on the rendered web page.

请注意,我们的标记结构位于<template> HTML元素中,该元素不会显示在呈现的网页上。

The web page should now display the custom element.

网页现在应显示自定义元素。

Image for post

Now, to prove that our custom styles are encapsulated and not affected by the global styles, we add global styles and a simple heading element to our index.html file.

现在,为了证明我们的自定义样式已封装并且不受全局样式的影响,我们向index.html文件中添加了全局样式和一个简单的heading元素。

The web page renders the two headings but with different styles.

网页呈现两个标题,但样式不同。

Image for post

You’ve just created a simple, custom web element with encapsulated style by using just vanilla javascript. Use it as a starting point and extend it to your preferences to create your own unique web elements.

您刚刚使用香草JavaScript创建了一个具有封装样式的简单自定义Web元素。 使用它作为起点并将其扩展到您的首选项,以创建自己的独特Web元素。

You can find the whole example here:

您可以在此处找到整个示例:

翻译自: https://levelup.gitconnected.com/vanilla-web-components-caa1bd83ed86


http://www.niftyadmin.cn/n/1468916.html

相关文章

java 16进制加减_【java解惑】十六进制加法问题

如下代码&#xff1a;public class Example005 {public static void main(String[] args) {System.out.println("out1" Long.toHexString(0x100000000L 0xcafebabe));System.out.println("out2" Long.toHexString(0x100000000L 0xcafebabeL));}}输出结果…

javascript 简介_教授javascript第1部分简介

javascript 简介It is additional online learning material for Professor JavaScript YouTube channel, Part 1: Introduction.它是JavaScript YouTube教授频道的第1部分&#xff1a;简介的其他在线学习材料。 Professor JavaScript is a JavaScript online learning courses…

raphaeljs_矢量图形与raphaeljs

raphaeljsRaphaelJS is a JavaScript library that provides an API for manipulating SVG, and SVG support for Internet Explorer. It achieves the latter by emulating SVG in Internet Explorer using VML.RaphaelJS是一个JavaScript库&#xff0c;它提供用于处理SVG的AP…

java for each 原理_Java for each实现机制代码原理解析

源测试代码如下public class ForEachTest {public void test4Iterate(Iterable strings) {for (String str : strings) {System.out.println(str);}}public void test4Array(String[] strings) {for (String str : strings) {System.out.println(str);}}}执行编译命令javac For…

在2020年提升React应用性能的10条技巧

There are different ways of solving the same problem when it comes to programming — whether it be JavaScript, React, Python, or any other language. When you look into someone else’s code, there is always something new that you can learn.解决同一问题时&am…

月亮js简介

Yet another JavaScript framework on the block. It seems like these frameworks are being churned out at an ever-increasing rate.是该模块上的另一个JavaScript框架。 这些框架似乎正在以越来越高的速度被淘汰。 The newest JavaScript framework I’ll be introducing…

javascript原型链_原型javascript

javascript原型链Correct way of adding functionality正确的功能添加方式 Managing memory while writing code is one of the major qualities a developer can possess. Execution environment executes javascript code in two stages, i.e Creation and Hoisting.在编写代…

java wrap()_Java中的FloatBuffer wrap()方法

可以使用wrap()java.nio.FloatBuffer类中的方法将float数组包装到缓冲区中。此方法需要一个参数&#xff0c;即将数组包装到缓冲区中&#xff0c;并返回创建的新缓冲区。如果返回的缓冲区被修改&#xff0c;则数组的内容也将被修改&#xff0c;反之亦然。演示此的程序如下所示-…