javascript - 在 phantomjs 中没有用于 document.register 的 polyfill

标签 javascript polyfills web-component

假设我正在编写以下 Web 组件或自定义元素:

<!doctype html>

<html>
  <head>
    <script src="js/MutationObserver.js"></script>
    <script src="js/CustomElements.js"></script>

    <script>
      var proto = Object.create(HTMLElement.prototype);

      proto.createdCallback = function() { console.log('created');};
      proto.enteredViewCallback = function() {};

      document.register('x-foo', {prototype: proto});

      document.createElement('x-foo');
    </script>
  </head>

  <body>
  </body>
</html>

( polyfill on github ) 这适用于 Chrome。但是当我尝试在 PhantomJS 中运行它时,出现以下错误:

TypeError: instanceof called on an object with an invalid prototype property.

http://localhost/~me/js/CustomElements.js:18
http://localhost/~me/js/CustomElements.js:217 in instantiate
http://localhost/~me/js/CustomElements.js:333
http://localhost/~me/js/CustomElements.js:342 in createElement
...

如果要复现,这里是我的phantomjs脚本

var page = require('webpage').create();
page.open('http://localhost/~me/test.html', function() {
  phantom.exit();
});

我已经尝试过其他的 polyfills

但实际上它们都不起作用。任何想法如何解决这个问题?

最佳答案

我遇到了同样的问题。它是用这个 polyfill 解决的: https://github.com/WebReflection/document-register-element

您需要使用 document.registerElement 方法。

关于javascript - 在 phantomjs 中没有用于 document.register 的 polyfill,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20668488/

相关文章:

javascript - API 上的 jQuery 不会自动绑定(bind)

javascript - 如何防止回调多次触发?

javascript - 当我在任何浏览器中将 bot 框架 v4 与 js 集成时,LUIS 不起作用

javascript - 切换自定义元素 Polymer 中的纸张对话框

web-component - 模板标签内的 html 和 body 标签

javascript - Canvas 描边和填充颜色

javascript - Safari 中是否有 window.performance.getEntriesByType 的 polyfill?

javascript - Lazyload polyfill/Magneto 2 js 错误 "loading-attribute-polyfill.js Uncaught ReferenceError: module is not defined"

启用 polyfill 后,Angular 6 路由器会在所有浏览器中崩溃应用程序?

html - polymer :将属性传递给子元素不起作用