【问题标题】:Polymer styling :host[attribute] failed聚合物样式:主机 [属性] 失败
【发布时间】:2016-09-02 10:18:09
【问题描述】:

得到带有faded 属性的简单组件。

 <template>
      <style>
            :host {
                display: block;
                height: 7em;
                background: white;
                border: 1px solid var(--paper-blue-grey-50);
                width: 100%;
                margin: 0.1em 0;
            }
            :host ([faded]) {
                display: none;
                background: #eaeaea;
                color: #a8a8a8;
                cursor: auto;
                pointer-events: none;
            }
            .month {
                ...
            }
            .names {
               ...
            }
            .date {
                ...
            }
        </style>

        <div>
            <div class="month" hidden="[[hideMonth]]">[[details.month]]</div>
            <div class="names">
                <span class="date">[[details.date]]</span>
                <div hidden="[[hideName]]">
                    <div>[[details.name]]</div>
                    <div class="highlight annotation"></div>
                </div>
            </div>
        </div>

    </template>

    <script>
        'use strict';
        Polymer({
            is: "calendar-day-short-view",
            properties: {
                date: {
                    type: Object,
                    observer: '_dayChanged'
                },
                details: {
                    type: Object,
                    value: function () {
                        return {}
                    }
                },
                hideMonth: {
                    type: Boolean,
                    reflectToAttribute: true,
                    value: false
                },
                hideName: {
                    type: Boolean,
                    reflectToAttribute: true,
                    value: false
                },
                holiday: {
                    type: Boolean,
                    reflectToAttribute: true,
                    value: false
                },
                faded: {
                    type: Boolean,
                    reflectToAttribute: true,
                    value: false
                }
            },
            _dayChanged: function () {
                var cal = new Calendar();
                cal.date = this.date;
                this.details = {
                    date: this.date.getDate(),
                    name: cal.getDayName(),
                    day: this.date.getDay(),
                    month: cal.getMonthName()
                };
            }
        })
    </script>
</dom-module>

不幸的是,当我尝试像这样将组件包含到另一个组件中时,它只呈现 :host 样式并忽略 :host[faded]

<template is="dom-repeat" items="[[week]]">
<calendar-day-short-view date="[[item]]" class="flex" hide-month faded></calendar-day-short-view>
</template>

【问题讨论】:

    标签: html css css-selectors polymer web-component


    【解决方案1】:

    你在这两个方面都错了。要使用的正确选择器是:host([faded])

    【讨论】:

    • 我没有意识到:host:host() 之间的区别。具体来说,这对 Polymer 是双向的,因为它将 :host[faded] 选择器替换为 x-foo[faded] 或其他。所以它与我假设的其他浏览器向后兼容。这里有一个例子:jsbin.com/zusayu/1/edit?html,output
    • @Tomasz Pluskiewicz:嗯,我没有意识到 Polymer 做到了。我认为这是一个错误,因为这种行为不符合规范。事实上,css-scoping has an example comparing the two,它非常清楚地表明复合选择器应该不匹配任何内容,而功能(非复合)变体将起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多