简体   繁体   English

如何继承手动扩展的聚合物元素的样式?

[英]How to inherit styles for manualy extended Polymer Element?

I would like to create new Polymer Element that extends native HTML Element (let's say div ) , so I could use <div is="my-div"> , but inherits behavior from another Polymer element ( my-list ). 我想创建扩展本机HTML元素(比如div )的新Polymer元素,因此我可以使用<div is="my-div"> ,但是从另一个Polymer元素( my-list )继承行为。 I have achieved it by: 我通过以下方式实现了它:

<polymer-element name="my-list">
    <template>
        <style>
            :host {
                background-color: #EEE;
                display: block;
                border: 1px solid black;
            }
        </style>
        <content></content>
    </template>
    <script>
        Polymer('my-list', {
            color: "red",
            ready: function() {
                console.info("my-list is ready!");
                this.children[0].style.color = this.color;
            }
        });
    </script>
</polymer-element>
<polymer-element name="my-div" extends="div">
    <script>
        (function() {
            var myListProto = Polymer.getRegisteredPrototype("my-list");
            var myDivProto = Polymer.extend({}, myListProto);
            myDivProto.color = "blue";
            Polymer('my-div', myDivProto);
        }());
    </script>
</polymer-element>

Working jsFiddle is here . 工作jsFiddle在这里

The problem is that my-list styles does not apply to my-div . 问题是my-list样式不适用于my-div

Is there any way to inherit them as well? 有没有办法继承它们呢? Or should I do such extension some other way? 还是应该以其他方式进行此类扩展?

It looks to me like what you are doing here is a mixin technique. 在我看来,您在这里所做的是一项混合技术。 You have two classes that are not on the same inheritance chain that you want to have share an API. 您有两个不在同一个继承链上的类,这些类要共享一个API。

You did the mixin part for the prototype, and you can do it for the template too, but there are various details. 您为原型完成了mixin部分,也可以为模板进行了混合,但是有很多细节。

Here is a JsBin showing how I would do it: http://jsbin.com/vuru/1/edit 这是一个显示我将如何执行的JsBin: http ://jsbin.com/vuru/1/edit

I framed it more directly as a mixin problem, which made it easier for me to work with. 我将其直接设计为一个混入问题,这使我更易于使用。

Key info I used: 我使用的主要信息:

  • If you define a registerCallback() in your Polymer prototype, it's called back at registration time with the prototype as this and a reference to the <polymer-element> as an argument. 如果定义registerCallback()中的聚合物的原型,它的在登记时间与原型作为回叫this和所述参考<polymer-element>作为参数。

  • you can override fetchTemplate to customize template selection 您可以覆盖fetchTemplate以自定义模板选择

  • If you don't install a template into the <polymer-element> before registerCallback() time, you have to invoke the shadow-dom CSS polyfill engine by hand (that's this bit: Platform.ShadowCSS.shimStyling(...) ). 如果没有在registerCallback()之前将模板安装到<polymer-element> ,则必须手动调用shadow-dom CSS polyfill引擎(这一点是: Platform.ShadowCSS.shimStyling(...) ) 。

Not exactly what you want but core-style lets you define a style you can easily include in your templates. 核心样式不完全是您想要的样式,您可以定义可以轻松包含在模板中的样式。 Then when you extend an element just be sure to include the same core-style in your child template. 然后,当您扩展元素时,请确保在子模板中包括相同的核心样式。

I messed around for a while trying to get the extended element into the child element via in the child-element but this didn't seem to work. 我一团糟试图将扩展元素通过子元素添加到子元素中,但这似乎没有用。

Anyways, core-style makes passing styles around pretty easy. 无论如何,核心样式使传递样式变得非常容易。 see here https://pascalprecht.github.io/2014/08/01/sharing-styles-across-web-components-with-polymer-and-core-style/ 看到这里https://pascalprecht.github.io/2014/08/01/sharing-styles-across-web-components-with-polymer-and-core-style/

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

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