Polymer

使用 web 组件构建现代应用程序。我们原始的 Web 组件库。(Build modern apps using web components. Our original Web Component library.)

Github星跟蹤圖

Polymer(聚合体)

Polymer 允许您构建封装的、可重用的 Web 组件,这些组件就像标准的 HTML 元素一样工作,用于构建 Web 应用程序。使用聚合物构建的 Web 组件非常简单,只需导入它的定义,然后像使用其他 HTML 元素一样使用它。

<!-- Import a component -->
<script src="https://unpkg.com/@polymer/paper-checkbox@next/paper-checkbox.js?module" type="module" ></script>
<!-- Use it like any other HTML element -->
<paper-checkbox>Web Components!</paper-checkbox>

现在,Web 组件是在 Safari 和 Chrome(已安装的浏览器的70%)上本地实现的,并且可以使用 polyfills 在 Firefox、Edge 和 IE11 上很好地运行。 在下面阅读更多内容。

入门指南

  • 试用 Polymer 的最简单方法是使用以下在线工具之一:
  • 您还可以将 此 HTML 文件 保存到本地文件,然后在支持 JavaScript 模块的任何浏览器中运行它。
  • 准备在项目中使用 Polymer 时,请通过 npm 安装它。要在浏览器中运行项目,需要模块兼容的工具链。我们建议如下安装 Polymer CLI 并使用其开发服务器。
    1. 将 Polymer 添加到您的项目中:npm i @polymer/polymer
    2. 通过扩展PolymerElement并在您的类中调用customElements.define来创建元素(请参见下面的示例)。
    3. 安装Polymer CLI:npm i -g polymer-cli
    4. 运行开发服务器并打开指向其URL的浏览器:polymer serve --npm
    Polymer 3.0 是使用 JavaScript 模块在 npm 上发布的。这意味着它可以利用所有当前主流浏览器中可用的标准本机 JavaScript 模块加载器。但是,由于 Polymer 使用 npm 约定按名称引用依赖关系,因此需要轻度转换才能将说明符重写为 URL,才能在浏览器中运行。 Polymer-cli 的开发服务器 polymer serve 以及 polymer build(用于构建用于部署的优化应用程序)都可以自动处理此转换。
    诸如 webpackRollup 之类的工具也可以用于服务和/或捆绑 Polymer 元素。

最小的例子

  1. 创建一个扩展 PolymerElement 的类。
  2. 实现一个静态属性(properties)获取器,该获取器描述元素的公共属性/属性API(这些将自动成为观察到的属性)。
  3. 然后实现一个模板(template)获取器,该返回器返回一个HTMLTemplateElement 来描述元素的呈现,包括封装的样式和所有属性绑定。
<script src="node_modules/@webcomponents/webcomponents-loader.js"></script>
  <script type="module">
    import {PolymerElement, html} from '@polymer/polymer';

    class MyElement extends PolymerElement {
      static get properties() { return { mood: String }}
      static get template() {
        return html`
          <style> .mood { color: green; } </style>
          Web Components are <span class="mood">[[mood]]</span>!
        `;
      }
    }

    customElements.define('my-element', MyElement);
  </script>

  <my-element mood="happy"></my-element>

概览

Polymer 是建立在基于 Web 标准的 Web Components API 之上的轻量级库,可以更轻松地构建您自己的自定义 HTML 元素。创建可重用的自定义元素 -- 并使用由其他元素构建的元素 -- 可以使构建复杂的Web应用程序更轻松,更高效。通过基于浏览器内置的 Web Components API(或需要多重填充),Polymer 元素可以在浏览器级别互操作,并可与其他与现代浏览器一起使用的框架或库一起使用。

在使用自定义元素的许多方法中,它们对于构建可重用的UI组件尤其有用。您可以使用 Polymer 来定义该元素,而不是在不同的框架和不同的项目中重新构建特定的导航栏或按钮,而是可以在整个项目或任何将来的项目中重用它。

Polymer 提供了一种声明性语法,可以轻松创建自己的自定义元素,使用所有标准Web技术 -- 使用 HTML 定义元素的结构,使用 CSS 进行样式化,并使用 JavaScript 添加与元素的交互。

Polymer 还提供可选的双向数据绑定,这意味着:

  • 当元素的模型中的属性得到更新时,该元素可以自动更新。
  • 当内部更新元素时,更改可以传播回模型。

Polymer 设计为灵活、轻便、靠近网络平台 -- 库不会发明复杂的新抽象和魔法,而是以直接的方式使用 Web 平台的最佳功能,以简单地创建自定义元素。

除了用于构建自己的自定义元素的 Polymer 库之外,Polymer 项目还包含一系列预先构建的元素,您可以将其放在页面上并立即使用,或者用作自己的自定义元素的起点。

(Second edition: vz revised at 2019.11.29)

概覽

名稱與所有者Polymer/polymer
主編程語言HTML
編程語言JavaScript (語言數: 4)
平台
許可證BSD 3-Clause "New" or "Revised" License
發布數158
最新版本名稱v3.5.1 (發布於 2022-06-03 14:55:30)
第一版名稱v0.0.20130711 (發布於 )
創建於2012-08-23 20:56:30
推送於2024-03-27 11:19:20
最后一次提交2022-06-03 14:55:57
星數22k
關注者數848
派生數2k
提交數6.9k
已啟用問題?
問題數3916
打開的問題數50
拉請求數1071
打開的拉請求數27
關閉的拉請求數357
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

Polymer

Build Status
Published on npm
Published on webcomponents.org

ℹ️ Note: This is the current stable version of the Polymer library. At Google I/O 2018 we announced a new Web Component base class, LitElement, as a successor to the PolymerElement base class in this library.

If you're starting a new project, we recommend that you consider using LitElement instead.

If you have a project you've built with an earlier version of the Polymer library, we recommend that you migrate to 3.0 for best compatibility with the JavaScript ecosystem. Thanks to the interoperability of Web Components, elements built with Polymer 3.0 and LitElement can be mixed and matched in the same app, so once you have updated your project to Polymer 3.0, you can migrate to LitElement incrementally, one element at a time. See our blog post on the Polymer Project roadmap for more information.

Polymer lets you build encapsulated, reusable Web Components that work just like standard HTML elements, to use in building web applications. Using a Web Component built with Polymer is as simple as importing its definition then using it like any other HTML element:

<!-- Import a component -->
<script src="https://unpkg.com/@polymer/paper-checkbox@next/paper-checkbox.js?module" type="module" ></script>

<!-- Use it like any other HTML element -->
<paper-checkbox>Web Components!</paper-checkbox>

Web Components are now implemented natively on Safari and Chrome (~70% of installed browsers), and run well on Firefox, Edge, and IE11 using polyfills. Read more below.

Getting started

  • The easiest way to try out Polymer is to use one of these online tools:

  • You can also save this HTML file to a local file and run it in any browser that supports JavaScript Modules.

  • When you're ready to use Polymer in a project, install it via npm. To run the project in the browser,
    a module-compatible toolchain is required. We recommend installing the Polymer CLI to and using its development server as follows.

    1. Add Polymer to your project:

      npm i @polymer/polymer

    2. Create an element by extending PolymerElement and calling customElements.define with your class (see the examples below).

    3. Install the Polymer CLI:

      npm i -g polymer-cli

    4. Run the development server and open a browser pointing to its URL:

      polymer serve --npm

    Polymer 3.0 is published on npm using JavaScript Modules.
    This means it can take advantage of the standard native JavaScript module loader available in all current major browsers.

    However, since Polymer uses npm conventions to reference dependencies by name, a light transform to rewrite specifiers to URLs is required to run in the browser. The polymer-cli's development server polymer serve, as well as polymer build (for building an optimized app for deployment) automatically handles this transform.

    Tools like webpack and Rollup can also be used to serve and/or bundle Polymer elements.

Minimal Example

  1. Create a class that extends PolymerElement.
  2. Implement a static properties getter that describes the element's public property/attribute API
    (these automatically become observed attributes).
  3. Then implement a template getter that returns an HTMLTemplateElement describing the element's rendering, including encapsulated styling and any property bindings.
  <script src="node_modules/@webcomponents/webcomponents-loader.js"></script>
  <script type="module">
    import {PolymerElement, html} from '@polymer/polymer';

    class MyElement extends PolymerElement {
      static get properties() { return { mood: String }}
      static get template() {
        return html`
          <style> .mood { color: green; } </style>
          Web Components are <span class="mood"></span>!
        `;
      }
    }

    customElements.define('my-element', MyElement);
  </script>

  <my-element mood="happy"></my-element>

Overview

Web components are an incredibly powerful new set of primitives baked into the web platform, and open up a whole new world of possibility when it comes to componentizing front-end code and easily creating powerful, immersive, app-like experiences on the web.

Polymer is a lightweight library built on top of the web standards-based Web Components APIs, and makes it easier to build your very own custom HTML elements. Creating reusable custom elements - and using elements built by others - can make building complex web applications easier and more efficient.

By being based on the Web Components APIs built in the browser (or polyfilled where needed), elements built with Polymer are:

  • Built from the platform up
  • Self-contained
  • Re-usable
  • Interoperable across frameworks

Among many ways to leverage custom elements, they can be particularly useful for building reusable UI components. Instead of continually re-building a specific navigation bar or button in different frameworks and for different projects, you can define this element once using Polymer, and then reuse it throughout your project or in any future project.

Polymer provides a declarative syntax to easily create your own custom elements, using all standard web technologies - define the structure of the element with HTML, style it with CSS, and add interactions to the element with JavaScript.

Polymer also provides optional two-way data-binding, meaning:

  1. When properties in the model for an element get updated, the element can update itself in response.
  2. When the element is updated internally, the changes can be propagated back to the model.

Polymer is designed to be flexible, lightweight, and close to the web platform - the library doesn't invent complex new abstractions and magic, but uses the best features of the web platform in straightforward ways to simply sugar the creation of custom elements.

About Polymer 3.0

Polymer 3.0 is now released to stable, and introduces a major change to how Polymer is distributed: from HTML Imports on Bower, to JS modules on npm. Otherwise, the API is almost entirely backward compatible with Polymer 2.0 (the only changes are removing APIs related to HTML Imports like importHref, and converting Polymer's API to be module-based rather than globals-based).

Migrating to Polymer 3.0 by hand is mostly mechanical:

  • Components should be defined in JS modules instead of in HTML
  • Templates should be encoded in JS modules using a static get template() getter on PolymerElement subclasses using the html tagged template literal function (which parses HTMLTemplateElements out of strings in JS) rather than using <template> elements in a <dom-module>
  • All dependencies should be imported JS module imports rather than HTML Imports.

However, the polymer-modulizer tool automates the vast majority of this migration work. Please see details on that repo for automated conversion of Polymer 2.0 apps and elements to Polymer 3.0.

? Looking for Polymer v2.x? Please see the the v2 branch.

? Looking for Polymer v1.x? Please see the the v1 branch.

Contributing

The Polymer team loves contributions from the community! Take a look at our contributing guide for more information on how to contribute. Please file issues on the Polymer issue tracker following the issue template and contributing guide issues.

Communicating with the Polymer team

Beyond GitHub, we try to have a variety of different lines of communication available:

License

The Polymer library uses a BSD-like license that is available here

去到頂部