Polymer: Reload Styles when loading dynamic content

℡╲_俬逩灬. 提交于 2019-12-11 01:35:12

问题


I'm including styles using <style include="style-name"></style> which generally works fine.

However, I have one element which dynamically loads and changes its contents via JavaScript. It will at some point load new content from some external source and update its contents accordingly. Those new contents do not have the style from style-name applied to them.

How do I get Polymer to reevaluate the styles and therefore also apply them to the new content?


I'm using Meteor + Synthesis if that makes a difference.


回答1:


For keep the style scope you should use Polymer importHref() and and dom() functions like this:

// ...
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));
// ...

This way polymer will apply the style scopes if you using the Shady DOM, in Shadow DOM it should be working by default, but I did not tested, so better to use the Polymer built in functions for that.

The Polymer.updateStyles() is only working when you changing the Local DOM css variables like this: this.customStyle['--my-toolbar-color'] = 'blue';



来源:https://stackoverflow.com/questions/39541782/polymer-reload-styles-when-loading-dynamic-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!