2つのカスタム要素(v1)を持つWebコンポーネントを使用して簡単な例を作成しました。 のindex.html:ネストされた要素(Webコンポーネント)がテンプレートを取得できません
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="import" href="app-container.html">
</head>
<body>
<app-container></app-container>
</body>
</html>
アプリ-container.html:
<link rel="import" href="toolbar.html">
<template id="app-container">
<app-toolbar></app-toolbar>
</template>
<script>
customElements.define('app-container', class extends HTMLElement {
constructor() {
super();
let shadowRoot = this.attachShadow({ mode: 'open' });
const content = document.currentScript.ownerDocument.querySelector('#app-container').content;
shadowRoot.appendChild(content.cloneNode(true));
}
});
</script>
toolbar.html:
しかしtoolbar.html document.currentScript
では、アプリと同じです-container.htmlとなるため、のテンプレートが見つかりません。querySelector('#app-toolbar')
この問題を解決するには?
クロム55(polyfillなし)でテストされた例。
スーパーシャープ、多くのありがとう!明けましておめでとうございます! –
ありがとう、あなたも! – Supersharp