我有一个shared-styles
元素可以保留我的大部分应用程序颜色。我可以轻松地手动更改颜色shared-styles.html
,如果我使用 CSS 变量,我的所有其他组件都可以从那里继承。
我的问题是我需要更新 CSS 变量,shared-styles.html
并让所有其他继承 CSS 变量的组件相应地更新它们的颜色。下面是我的shared-styles.html
。为简洁起见,我删除了除--app-primary-color
.
<link rel="import" href="../bower_components/polymer/polymer-element.html">
<!-- shared styles for all views -->
<dom-module id="shared-styles">
<template>
<style is="custom-style">
:host {
--app-primary-color:#2196F3;
}
</style>
</template>
<script>
class SharedStyles extends Polymer.Element {
static get is() { return 'shared-styles'; }
ready(){
super.ready();
console.log('update css');
this.updateStyles({'--app-primary-color': 'red'});
}
}
window.customElements.define(SharedStyles.is, SharedStyles);
</script>
</dom-module>
这就是我将它们包含在其他组件中的方式。例如:
<dom-module id="test-element">
<template>
<style include="shared-styles">