The implementation of the "Polymer" framework in the Java class library and the analysis of their technical principles
Polymer is a JavaScript library based on the Web Components standard, which is used to build a reusable custom element and web component.Its design goal is to simplify front -end development and provide a method to quickly build a modern web application.
Polymer uses a technology called Shadow DOM to realize the packaging and style isolation of custom elements.Shadow Dom allows developers to create components with their own independent style and behavior, avoiding the problem of style conflict and naming space pollution.With Shadow Dom, Polymer can create a separate DOM sub -tree for each custom element and encapsulate it in a "shadow" DOM.In this way, the style and behavior of custom elements will not affect external pages or other components.
In addition to Shadow Dom, Polymer also uses Web Components standards such as HTML Imports, HTML Templates, and Custom Elements to achieve the creation and combination of custom elements.HTML Imports allows developers to import other HTML files in one HTML file to introduce the definition and dependence of custom elements.HTML Templates provides a method for defining templates in HTML to create the appearance and structure of custom elements.CUSTOM ELEMENTS defines a way to expand HTML elements, enabling developers to create new HTML tags as needed and define their behaviors and styles for them.
Below is a simple example code that demonstrates how to use Polymer to create a simple custom element:
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<Title> Polymer Example </Title>
<!-Import Polymer related files->
<script src="https://unpkg.com/@webcomponents/webcomponentsjs@^2/dist/webcomponents-bundle.js"></script>
<link rel="import" href="https://cdn.jsdelivr.net/npm/@polymer/polymer@^3.0.0-preview.3/polymer-element.html">
</head>
<body>
<!-Define a custom element->
<dom-module id="my-element">
<template>
<style>
:host {
display: block;
padding: 16px;
background-color: pink;
color: white;
}
</style>
<h1>Hello, Polymer!</h1>
</template>
<script>
// Inherit the Polymer.element class to create custom elements
class MyElement extends Polymer.Element {
static get is() { return 'my-element'; }
}
customElements.define(MyElement.is, MyElement);
</script>
</dom-module>
<!-Use custom elements in the page->
<my-element></my-element>
</body>
</html>
In the above code, we define a custom element called `My-Element`.The internal structure and style of the custom element are defined using the `Template>` tags. The code in the label in the label inherits the `Polymer.element` and declares the label name of this element.
Through the `CustomLements.define` method, we register the definition of custom elements into the browser, so that` <my-Element> `can be used on the page.
The above examples simply demonstrate the basic usage of Polymer. In fact, Polymer also provides many powerful functions, such as data binding, event processing, template rendering, etc., which can greatly simplify the work of front -end development.
In short, Polymer's implementation relies on Web Components standards and through technologies such as Shadow Dom, HTML Imports, HTML TEMPLES and Custom Elements to achieve reusable custom elements and web components, providing developers with a rapid construction of a modern web applicationProgram method.