【问题标题】:Polymer: Reload Styles when loading dynamic contentPolymer:加载动态内容时重新加载样式
【发布时间】:2017-01-25 06:58:16
【问题描述】:

我使用<style include="style-name"></style> 包含样式,这通常可以正常工作。

但是,我有一个元素可以通过 JavaScript 动态加载和更改其内容。它会在某个时候从某个外部源加载新内容并相应地更新其内容。这些新内容没有应用style-name 的样式。

如何让 Polymer 重新评估样式,从而将它们应用到新内容中?


如果有影响的话,我正在使用 Meteor + Synthesis。

【问题讨论】:

  • 您是否尝试致电Polymer.updateStyles()this.updateStyles()?请参阅here 了解更多信息

标签: css meteor polymer polymer-1.0 web-component


【解决方案1】:

为了保持样式范围,您应该使用 Polymer importHref() 和 dom() 函数,如下所示:

// ...
let importSource = Polymer.Base.importHref('path/to/external-source',
// Import is done, use the body of that
event => {
    // #container is my element which should have the content
    // but you can change to any element what you want
    // or use just the this.root
    Polymer.dom(this.$.container).innerHTML = importSource.import.body;
},
// Handle errors
event => console.error(event));
// ...

如果您使用 Shady DOM,这种方式聚合物将应用样式范围,在 Shadow DOM 中它应该默认工作,但我没有测试,所以最好使用 Polymer 内置函数。

Polymer.updateStyles() 仅在您像这样更改本地 DOM css 变量时才有效:this.customStyle['--my-toolbar-color'] = 'blue';

【讨论】:

  • 重要的部分是使用Polymer.dom(element).innerHTML = "..."; 而不仅仅是element.innerHTML。谢谢:)
  • @Yorrd Im 导入了包含自定义元素的模板文件,这就是我使用 importHref 的原因 :) 不客气!
猜你喜欢
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 2019-06-30
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 2010-10-30
  • 2012-08-03
相关资源
最近更新 更多