<head><!-- Load the polyfills first --><script src="https://unpkg.com/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script><!-- Then afterwards, load components --><script type="module"src="./superlative-input.js"></script></head>
<!-- Load all polyfills, including template, Promise, etc. --><!-- Useful when supporting IE11 --><script src="https://unpkg.com/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script><!-- Load only the Shadow-DOM and Custom Elements polyfills --><!-- Useful to support Firefox <63 --><script src="https://unpkg.com/@webcomponents/webcomponentsjs/entrypoints/webcomponents-sd-ce-index.js"></script><!-- Load only the Shadow-DOM polyfills --><script src="https://unpkg.com/@webcomponents/webcomponentsjs/entrypoints/webcomponents-sd-index.js"></script><!-- Load only the Custom Elements polyfills --><script src="https://unpkg.com/@webcomponents/webcomponentsjs/entrypoints/webcomponents-ce-index.js"></script>
大多数情况下,您需要同步加载webcomponents-loader.js位于 顶部的脚本head。但有时您需要异步加载。例如:如果您的应用实现了静态应用外壳,以便给用户带来性能方面的视觉冲击,您会希望静态 HTML 和 CSS 能够尽快加载,这意味着要消除阻塞渲染的资源。在这种情况下,您需要使用window.WebComponents.waitFor方法来确保组件在 polyfill 之后加载。以下是
无偿解除README中稍作修改的示例webcomponentsjs:
<!-- Note that because of the "defer" attr, "loader" will load these async --><script defersrc="node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script><!-- Load a custom element definitions in `waitFor` and return a promise --><!-- Note that all modules are deferred --><script type="module">WebComponents.waitFor(()=>// At this point we are guaranteed that all required polyfills have// loaded, and can use web components API's.// The standard pattern is to load element definitions that call// `customElements.define` here.// Note: returning the import's promise causes the custom elements// polyfill to wait until all definitions are loaded and then upgrade// the document in one batch, for better performance.Promise.all([import('./my-element.js'),import('/node_modules/bob-elements/bobs-input.js'),import('https://unpkg.com/@power-elements/lazy-image/lazy-image.js?module'),]));</script><!-- Use the custom elements --><my-element><bobs-inputlabel="Paste image url"onchange="e => lazy.src = e.target.value"></bobs-input><lazy-imageid="lazy"></lazy-image></my-element>
或者更典型的静态应用程序外壳模式的示例:
<head><script defersrc="https://unpkg.com/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script><style>/* critical static-app-shell styles here */</style></head><body><script type="module">// app-shell.js in turn imports its own dependenciesWebComponents.waitFor(()=>import('./app-shell.js'))</script><app-shellloading><headerid="static-header"><spanid="static-hamburger"></span><spanid="static-user"></span></header><main><divid="static-spinner"></div></main><footerid="static-footer"></footer></app-shell></body>
consttemplate=document.createElement('template')template.innerHTML=/*...*/// Let's give the polyfill a leg-upwindow.ShadyCSS&&window.ShadyCSS.prepareTemplate(template,'awesome-button')customElements.define('awesome-button',classAwesomeButtonextendsHTMLElement{constructor(){super()this.onclick=()=>report('Clicked on Shadow DOM')}connectedCallback(){// Let's give the polyfill a leg-upwindow.ShadyCSS&&window.ShadyCSS.styleElement(this)if (!this.shadowRoot){this.attachShadow({mode:'open'});this.shadowRoot.appendChild(template.content.cloneNode(true))}}})
看到那个ShadyCSS引用了吗?这是 polyfill 的一部分,它模拟了不支持 shadow DOM 的浏览器中的样式作用域。为了确保你的样式作用域正确,需要遵循以下几条规则:
<!-- This loads the app as a module on Chrome, Edge, Firefox, and Safari --><!-- Modules are always nonblocking, and they load after regular scripts, so we can put them first --><script type="module"src="/index.js"></script><!-- This loads the app on IE11 --><script nomodulesrc="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.26.0/polyfill.min.js"></script><!-- Take a look at rollup.config.js to see how to build this guy --><script nomodulesrc="./index.nomodule.js"></script>