简体   繁体   English

如何使用Polymer扩展多个元素

[英]How to extend multiple elements with Polymer

I know there is this question on multiple inheritance/composition . 我知道有关于多重继承/组合的这个问题。 However, it seems like this question is more about how to reuse functionality from multiple existing elements in other elements. 但是,似乎这个问题更多的是关于如何从其他元素中的多个现有元素重用功能。 And obviously, the solution for that are mixins. 显然,解决方案就是mixins。

I would like to know, how I can actually "decorate" existing elements without really borrow functionality from them. 我想知道,如何在不真正借用现有元素的情况下实际“装饰”现有元素。 We know there is this extends property one can use to extend an existing element with Polymer. 我们知道可以使用这个extends属性来扩展Polymer的现有元素。

So making a normal <button> behave like a mega-button is as simple as attaching <button is="mega-button"> and write a component for it. 因此,使普通的<button>表现得像一个mega-button就像附加<button is="mega-button">并为其编写一个组件一样简单。 But it turns out, that it's not possible to extend multiple elements. 但事实证明, extend多个元素是不可能的。 So something like extends="foo bar" doesn't work. 所以像extends="foo bar"类的东西不起作用。 What if I want to build a web component, that can actually be applied to different elements? 如果我想构建一个可以实际应用于不同元素的Web组件,该怎么办?

For example, I don't want to only extend <button> elements with mega-button but probably also an <a> element so that it looks like and behaves like a mega-button too? 例如,我不想只使用mega-button扩展<button>元素,但也可能只是一个<a>元素,以便它看起来像行为像一个mega-button

The mixin approach doesn't really help here (as far as I get it), because they do nothing more then providing shared logic for different web components. mixin方法在这里并没有真正帮助(据我所知),因为它们不再为不同的Web组件提供共享逻辑。 That means, you create multiple components, and reuse logic (packed in a mixin) from a mixin. 这意味着,您可以创建多个组件,并从mixin重用逻辑(打包在mixin中)。

What I need is a way to create one web component that can be applied to multiple elements. 我需要的是一种创建一个可以应用于多个元素的Web组件的方法。

Any idea how to solve that? 知道如何解决这个问题吗?

UPDATE UPDATE

Addy answered with some approaches to handle that use case. Addy回答了一些处理该用例的方法。 Here's a follow up question based on one approach 这是基于一种方法的后续问题

How to find out what element is going to be extended, while registering my own in Polymer 如何在Polymer中注册我自己的元素时,如何找出要扩展的元素

And another one on Is it possible to share mixins across web components (and imports) in Polymer? 另一个是否有可能在Polymer中分享Web组件(和导入)中的mixin?

UPDATE 2 更新2

I've written an article and concludes my experiences and learnings about inheritance and composition with polymer: http://pascalprecht.github.io/2014/07/14/inheritance-and-composition-with-polymer/ 我写了一篇文章,总结了我对聚合物遗传和组成的经验和学习: http//pascalprecht.github.io/2014/07/14/inheritance-and-composition-with-polymer/

If you need to have just a single import that has support for being applied to multiple elements, your element could include multiple element definitions which may or may not take advantage of Polymer.mixin in order to share functionality between your decorating elements. 如果您只需要一个支持应用于多个元素的导入,那么您的元素可能包含多个元素定义,这些定义可能会也可能不会利用Polymer.mixin来共享您的装饰元素之间的功能。

So pascal-decorator.html could contain Polymer element definitions for <pascal-span> and <pascal-button> , both of which mixin logic from some object defined within pascal-decorator.html . 所以pascal-decorator.html可以包含<pascal-span><pascal-button> Polymer元素定义,两者都是pascal-decorator.html定义的某个对象的逻辑混合。 You can then do <button is="pascal-button"> and <button is="pascal-span"> whilst the logic for doing so remains inside the same import. 然后你可以执行<button is="pascal-button"><button is="pascal-span">同时这样做的逻辑仍然在同一个导入中。

The alternative (if you strictly want to do this all in one custom element, which imo, makes this less clean) is to do something like checking against the type of element being extended, which could either be done in the manner you linked to or by checking as part of your element registration process. 替代方案(如果您严格要在一个自定义元素中执行此操作,imo,使其不那么干净)是执行诸如检查要扩展的元素类型之类的操作,这可以以链接到的方式完成,也可以通过检查元素注册过程的一部分。

In general, I personally prefer to figure out what logic I may need to share between elements that could be decorated, isolate that functionality into an element and then just import them into dedicated elements that have knowledge about the tag (eg <addy-button> , <addy-video> etc). 一般来说,我个人更喜欢弄清楚我可能需要在可以修饰的元素之间共享什么逻辑,将该功能分离为元素然后将它们导入到具有标记知识的专用元素中(例如<addy-button><addy-video>等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM