我正在編寫一個基本的聚合物組件,該組件應該也能夠在屬性更改時更改其樣式。無法訪問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