我正在编写一个基本的聚合物组件,该组件应该也能够在属性更改时更改其样式。无法访问Polymer Element中的“自定义样式”资源
当我尝试更改主题文件时,我使用id来检索它,但我总是得到空值。
这里是我的代码:
[other polymer imports]
<link rel="import" href="/themes/my-theme.html" id="current_theme"/>
<dom-module id="my-app">
<template>
<style>
:host{
font-family: 'Roboto', 'Noto', sans-serif;
-webkit-font-smoothing: antialiased;
}
:host ::content app-header{
background: var(--app-primary-color);
color: var(--app-text-color);
}
:host ::content user-app-toolbar{
background: var(--app-primary-color);
color: var(--app-text-color);
}
...
</style>
...
<script>
Polymer({
is: 'my-app',
properties: {
state: {
type: String,
reflectToAttribute: true,
observer: '_stateChanged',
},
},
_stateChanged: function(page) {
// get theme file
theme.href = '/theme/red.html';
}
});
</script>
</template>
</dom-module>
我-theme.html
<style is="custom-style">
my-app {
--app-primary-color: grey;
--app-secondary-color: black;
--app-text-color: white;
}
</style>
的问题是如何实现 “让主题文件”。我尝试了很多东西:
- document.querySelector(“#current_theme”): [返回null,我想这是因为它使用的主文档而不是元素的一个]
- 聚合物(DOM ).querySelector( 'current_theme'):[未定义]
- 这个$$ [ “current_theme”] [未定义]
如何做到这一点任何想法?
PS:以这种方式改变主题的想法是从这个stack overflow question
我也试过这个。$。current_theme但它说undefined。我忘了提及我的元素是在index.html文件中使用的 – Incio
我不确定是否可以尝试做什么,但至少检查包含路径。最初的文件驻留在/ themes /下,而在尝试从/ theme /中加载更改后。 – craPkit
好点。不幸的是,“/ themes /”路径是正确的,并且my-theme.html文件被浏览器正确加载。 “/ theme /”是一个拼写错误,但重点是我永远不会去那行代码,因为我检查(主题)[不是写在上面的代码中],它总是表示为null或undefined。 – Incio