简体   繁体   English

如何将第三方js库(BoxCast)嵌入到Polymer 3.0中

[英]How to embed third party js library (BoxCast) into Polymer 3.0

I'm trying to include the BoxCast JS API ( http://boxcast.github.io/boxcast_js_docs/examples/ ) into my polymer 3.0 web application as a third party library. 我正在尝试将BoxCast JS API( http://boxcast.github.io/boxcast_js_docs/examples/ )作为第三方库包含在我的聚合物3.0 Web应用程序中。

Ive managed to get the starter script / example from the above link running as a stand-alone html/js. 我设法从上面的链接获得起始脚本/示例作为独立的html / js运行。 But if I try to embed it in a Polymer Element the boxCast player fails with an Uncaught DOMException: Failed to execute 'querySelectorAll' on 'Document': '[object HTMLDivElement]' is not a valid selector error. 但是如果我尝试将它嵌入到Polymer Element中,则boxCast播放器会因Uncaught DOMException: Failed to execute 'querySelectorAll' on 'Document': '[object HTMLDivElement]' is not a valid selector错误。

Basic html - loads the player successfully 基本html - 成功加载播放器

<div id="testplayer"></div>
<script src="https://js.boxcast.com/v3.min.js"></script>
<script>
boxcast('#testplayer').loadChannel('vgniwkahiegcflco2pq6', {
  autoplay: false,
  showTitle: true,
  showDescription: true
});
</script>

Embedded in Polymer - doesn't work 嵌入聚合物 - 不起作用

import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
import './shared-styles.js';

import 'https://js.boxcast.com/v3.min.js';

class AruBoxCastPlayer extends PolymerElement {
  static get template() {
    return html`
      <style include="shared-styles">
      </style>
      <div id="testplayer">
        loading boxcast player
      </div> 
    `;
  }

  static get properties() {
    return {
    };
  }

  _loadBroadcast() {
    console.log("load broadcast");
    console.log('test node binding', this.$.testplayer.textContent);

    boxcast(this.$.testplayer).loadChannel('vgniwkahiegcflco2pq6', {
      autoplay: false,
      showTitle: true,
      showDescription: true
    });
  }

  ready() {
    super.ready();
    this._loadBroadcast();
  }
}

window.customElements.define('aru-boxcast-player', AruBoxCastPlayer);

My assumption is that I do something wrong with passing the div -element to the boxcast-js - as also the error message implies, but I can't figure out how to solve this issue. 我的假设是我将div -element传递给boxcast-js时出错了 - 正如错误信息所暗示的那样,但我无法弄清楚如何解决这个问题。 Thanks in advance for your help. 在此先感谢您的帮助。

Have you tried to use the es-6 way of importing libraries... 您是否尝试使用es-6导入库的方式...

# First, install the SDK from NPM
npm install boxcast-sdk-js --save

# Then, in your javascript project:
import boxcast from 'boxcast-sdk-js';

I'm not sure this will work but similar libraries like animejs, and jquery are updating to ES-6 and import similarly 我不确定这会有效但像animejs和jquery这样的类似的库正在更新到ES-6并导入类似的

// example
import anime from 'animjs';
import * as $ from 'jquery';

and from looking at the changelog they just updated their get* methods to ES-6... 从查看更改日志,他们刚刚将他们的get *方法更新为ES-6 ...

http://boxcast.github.io/boxcast_js_docs/changelog/ http://boxcast.github.io/boxcast_js_docs/changelog/

I don't know boxcast. 我不知道boxcast。

However, based on the first code : 但是,基于第一个代码:

boxcast('#testplayer').loadChannel

And the error returned in your polymer attempt, looks like boxcast method expects a string that allows it to locate the element that will be the base for it. 并且在聚合物尝试中返回的错误,看起来像boxcast方法需要一个字符串,允许它找到将作为它的基础的元素。

However, in the polymer version: 但是,在聚合物版本中:

boxcast(this.$.testplayer)

You are using $.testplayer that is not a string, but already an element. 您使用的$ .testplayer不是字符串,但已经是一个元素。

May be you can call directly loadChannel on this element 也许你可以在这个元素上直接调用loadChannel

this.$.testplayer.loadChannel (.....

Failing that, you should see in the boxcast docs how to skip the first step (trying to locate the element) 如果不这样做,你应该在boxcast docs中看到如何跳过第一步(试图找到元素)

Failing that, you could go along the first idea (call boxcast('#id') but the element should be probably in lightdom and not in shadowdom 如果做不到这一点,你可以继续第一个想法(调用boxcast('#id'),但元素可能应该在lightdom中,而不是在shadowdom中

Why is this the case ? 为什么会这样? - the message error states - 消息错误状态

Failed to execute 'querySelectorAll' on 'Document' 无法在'Document'上执行'querySelectorAll'

So the library is trying to find the element starting from document . 所以该库正在尝试从文档开始查找元素。 Since the polymer element will be rendered in shadow-dom, it is not accesible from the root of the document. 由于聚合物元素将以阴影方式呈现,因此无法从文档的根目录访问它。

Setting the element with the id testplayer outside of the polymer element, and the assigning it a slot inside the element should make it accesible from the document, but rendered inside the element. 在聚合物元素之外设置具有id testplayer的元素,并在元素内部分配一个槽应该使它可以从文档中访问,但是在元素内部呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM