Skip to content

Instantly share code, notes, and snippets.

@bgrins
Last active March 6, 2018 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgrins/0b139d1af466f560c8917005c911475b to your computer and use it in GitHub Desktop.
Save bgrins/0b139d1af466f560c8917005c911475b to your computer and use it in GitHub Desktop.
Lazy Custom Element registrations
<script src="app.js"></script>
<element-1 />
<element-2 />
// Option 1 - Module path:
// Pass a module path and an export name which will lazily get imported
// when that tag name is enountered.
customElements.define('element-1', './element1', 'Element1');
customElements.define('element-module', './element-module.html');
// Option 2 - Custom Handler function:
// Gets called when a valid custom element name is encountered *and* there
// isn't already a registration
customElements.definitionHandler((name) => {
if (name == "element-2") {
import {Element2} from "./element2";
return Element2;
}
});
export class Element1 extends HTMLElement {
connectedCallback() {
}
}
export class Element2 extends HTMLElement {
connectedCallback() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment