【问题标题】:Use <paper-material> element within custom element在自定义元素中使用 <paper-material> 元素
【发布时间】:2015-06-26 16:37:57
【问题描述】:

我正在使用 Polymer Starter 工具包并正在创建一个嵌套的自定义元素。我有一个外部元素可以多次“输出”内部元素。

我的问题是内部元素(名片)包含&lt;paper-material&gt;。此元素不受全局样式的影响。我知道 Polymer 向元素添加了一个 scoped-style 类,以确保它只能影响本地 DOM。在 Dev Tools 中删除 scoped-style 类会应用全局样式。

如何将标准 &lt;paper-element&gt; 中的样式应用到我的嵌套元素或在我的自定义元素中包含这些相同的样式。

编辑

看来我的问题是“app-theme”中的样式未应用于内部元素。如果我复制&lt;paper-element&gt; 样式(包括媒体查询)并遵循@Zikes 的答案,我可以获得预期的结果。

当元素已经完美时,复制元素中的所有内容似乎违背了聚合物的模块化特性。我错过了什么吗?

business-card.html

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/paper-material/paper-material.html">

<dom-module id="business-card">

  <style>
    :host {
      display: block;
    }
  </style>

  <template>
    <paper-material>
      <content></content>
    </paper-material>
  </template>

</dom-module>

<script>
(function() {
  Polymer({
    is: 'business-card'
  });
})();
</script>

非常感谢任何帮助

【问题讨论】:

  • 我也遇到了这个问题。 paper-material 在 index.html 中可以正常工作,但如果在元素中使用,样式会下降。奇怪的是,海拔属性仍然有效,只是被丢弃的 css。我希望导入元素的 html 文件(聚合物和纸张材料)会起作用,但它似乎不起作用。

标签: polymer polymer-1.0


【解决方案1】:

Polymer 保护元素内部免受文档样式的影响,反之亦然。这就是 CSS 作用域,它是 Web 组件的一个突出特性。

在简单的示例中可能会出现问题,但组件样式不会相互影响,文档样式不会无意中弄乱组件,这通常对组件重用非常有益。

Polymer Starter Kit 不适合在其他范围内使用 app-theme.html,但您可以做的一件事是将要使用的样式规则复制到 CSS 文件中,然后将该 CSS 文件导入到您的元素代码中如下。导入和样式被有效地使用(例如,导入只加载一次,即使你在多个元素中使用它)。

<dom-module id="business-card">

  <link rel="import" type="css" href="theme-styles.css">

  <style>
    :host {
      display: block;
    }
  </style>

  <template>
    <paper-material>
      <content></content>
    </paper-material>
  </template>

  <script>
    Polymer({
      is: 'business-card'
    });
  </script>

</dom-module>

现场示例:http://jsbin.com/hojajo/edit?html,output

【讨论】:

    【解决方案2】:

    如果您想将paper-material 效果直接应用于您的元素,您可以这样做:

    <link rel="import" href="../polymer/polymer.html">
    <link rel="import" href="../paper-styles/shadow.html">
    
    <dom-module id="business-card">
      <style>
        :host {
          display: block;
          position: relative;
          @apply(--shadow-transition);
        }
        :host([elevation="1"]) {
          @apply(--shadow-elevation-2dp);
        }
        :host([elevation="2"]) {
          @apply(--shadow-elevation-4dp);
        }
        :host([elevation="3"]) {
          @apply(--shadow-elevation-6dp);
        }
        :host([elevation="4"]) {
          @apply(--shadow-elevation-8dp);
        }
        :host([elevation="5"]) {
          @apply(--shadow-elevation-16dp);
        }
      </style>
      <template>
        <content></content>
      </template>
    </dom-module>
    
    <script>
      Polymer({
        is: 'business-card',
        properties: {
          /**
           * The z-depth of this element, from 0-5. Setting to 0 will remove the
           * shadow, and each increasing number greater than 0 will be "deeper"
           * than the last.
           *
           * @attribute elevation
           * @type number
           * @default 1
           */
          elevation: {
            type: Number,
            reflectToAttribute: true,
            value: 1
          },
          /**
           * Set this to true to animate the shadow when setting a new
           * `elevation` value.
           *
           * @attribute animated
           * @type boolean
           * @default false
           */
          animated: {
            type: Boolean,
            reflectToAttribute: true,
            value: false
          }
        }
      });
    </script>
    

    这是从paper-material 代码本身复制而来的:https://github.com/PolymerElements/paper-material/blob/master/paper-material.html

    【讨论】:

    • 我进行了更改,但我在浏览器中具有相同的最终效果。看来问题是内部 没有像我预期的那样受到应用程序主题的影响。我会尽快更新问题。
    猜你喜欢
    • 2016-10-17
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多