3

我试图简单地将它加载到我呈现的 HTML 中,但这不起作用。

当前代码:

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

1 回答 1

6

从聚合物松弛通道获得了解决方案(归功于威斯布鲁克):

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);
于 2019-04-15T16:05:24.050 回答