Takanori Oki
Frontend Developer
Takanori Oki @roppongi.js #4
HTML Imports
ES Modules
Almost 100% backward compatible with Polymer 2.x
<link rel="import" href="/bower_components/polymer/polymer-element.html">
<dom-module id="x-custom">
<template>
<style>
/* CSS rules for your element */
</style>
<div>{{greeting}}</div> <!-- data bindings in local DOM -->
</template>
<script>
class XCustom extends Polymer.Element {
static get is() { return 'x-custom'; }
}
customElements.define(XCustom.is, XCustom);
</script>
</dom-module>
import {PolymerElement, html} from '@polymer/polymer';
class LikeableElement extends PolymerElement {
static get properties() { return { liked: Boolean }}
static get template() {
return html`
<style>
.response { margin-top: 10px; }
</style>
<div hidden$="[[!liked]]" class="response">Web components like you, too.</div>
`;
}
}
customElements.define('likeable-element', LikeableElement);
<!DOCTYPE html>
<html lang="en">
<head>
<script
src="./node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"
>
</script>
<script
src="./node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"
>
</script>
</head>
<body>
<likeable-element></likeable-element>
</body>
</html>
<script src="node_modules/@webcomponents/webcomponents-bundle.js"></script>
<script type="module">
import {LitElement, html} from '@polymer/lit-element';
class MyElement extends LitElement {
static get properties() { return { mood: String }}
_render({mood}) {
return html`<style> .mood { color: green; } </style>
Web Components are <span class="mood">${mood}</span>!`;
}
}
customElements.define('my-element', MyElement);
</script>
<my-element mood="happy"></my-element>
By Takanori Oki