Итак, я попробовал следующий компонент:
import-test.js
import { html, PolymerElement } from '@polymer/polymer/polymer-element';
import {htmlLiteral} from '@polymer/polymer/lib/utils/html-tag.js';
import CssHtmlLoader from './cssHtmlLoader';
export default class ImportTest extends PolymerElement {
static get properties() {
return {
htmlTemplate:{
type:String,
notify:true,
reflectToAttribute: true
},
helloWorld:{
type:String,
notify:true,
reflectToAttribute: true,
value: 'auto'
}
}
}
constructor(){
super();
var that = this;
this.attachShadow({mode: 'open'});
console.log(this.shadowRoot);
this.htmlTemplate = CssHtmlLoader.prototype.getHtmlTemplate('templaten.html');
this.htmlTemplate.then((template) => {
console.log(that);
that.shadowRoot.innerHTML = template
});
}
}
customElements.define('import-test', ImportTest);
со следующим шаблоном html и css:
style.css:
p {
color: blue;
}
template.html:
link rel="stylesheet" href="style.css">
<p>Hello from template [[helloWorld]]</p>
и это был мой вывод:
Hello PolymerImportTest-app!
Hello from template [[helloWorld]](this is blue so the css import works)
поэтому моя проблема заключалась в том, что я не смог привязать свойства к html-шаблону.
И решение этой проблемы заключалось в том, чтобы поместить html-шаблон в отдельный файл javascript, и оттуда я смог экспортироватьи используйте его в пользовательском компоненте (с работающей привязкой свойств).
Вот мое решение:
import-test.js:
import { html, PolymerElement } from '@polymer/polymer/polymer-element';
import {template} from './template';
export default class ImportTest extends PolymerElement {
static get properties() {
return {
helloWorld:{
type:String,
notify:true,
reflectToAttribute: true,
value: 'auto'
}
}
}
constructor(){
super();
}
static get template() {
return template;
}
}
customElements.define('import-test', ImportTest);
template.js:
import {html} from '@polymer/polymer/polymer-element';
export const template=html`
<link rel="stylesheet" href="/src/PolymerImportTest-app/style.css">
<p>Hello from template [[helloWorld]]</p>
`;
style.css:
p {
color: blue;
}
И вывод:
Hello PolymerImportTest-app!
Hello from template auto(blue and the property is correctly attached)