点亮元素加载外部脚本

时间:2019-04-15 10:07:23

标签: web-component lit-element

我试图将其简单地加载到渲染的html中,但这不起作用。

当前代码:

render() {
    return html`<script @onload="${this.mapKitLoadedCallback}" src="https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js"></script>`;
  }

1 个答案:

答案 0 :(得分:0)

从聚合物松弛通道中获得解决方案(贷给Westbrook):

import { LitElement, html } from 'lit-element';

class MyElement extends LitElement {
  script() {
    let script = document.createElement('script');
    script.onload = this.onLoad.bind(this);
    script.src = 'https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js';
    return script;
  }
  onLoad() {
    alert('loaded');
  }
  render() {
    return html`
      <p>Hello world! From my-element</p>
      ${this.script()}
    `;
  }
}

customElements.define('my-element', MyElement);