【问题标题】:A web component with ECMA6带有 ECMA6 的 Web 组件
【发布时间】:2015-02-19 00:50:01
【问题描述】:

index.html

<!DOCTYPE html>
<html>
    <head>
        <script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
        <script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
        <script>
            traceur.options.experimental = true;
        </script>
        <link rel="import" href="x-item.html">
    </head>
    <body>
        <x-item></x-item>
    </body>
</html>

和我的网络组件: x-item.html

<template id="itemtemplate">
    <span>test</span>
</template>

<script type="module">
    class Item extends HTMLElement {
        constructor() {
            let owner = document.currentScript.ownerDocument;
            let template = owner.querySelector("#itemtemplate");
            let clone = template.content.cloneNode(true);
            let root = this.createShadowRoot();
            root.appendChild(clone);
        }
    }
    Item.prototype.createdCallback = Item.prototype.constructor;
    Item = document.registerElement('x-item', Item);
</script>

我没有收到任何错误,也没有收到我期望显示的内容,知道这是否真的有效吗?

这是如何在 ECMA6 语法中扩展 HTMLElement 的吗?

E:将它完全放在一个页面中可以解决问题,至少现在我知道它是创建自定义组件的正确方法,但问题是将它放在一个单独的文件中,我认为它有与traceur如何处理&lt;link rel="import" href="x-item.html"&gt;有关,我尝试将type属性添加到导入中,但没有成功。

【问题讨论】:

  • 而您期望显示的是……什么?寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误和必要的最短代码 在问题本身中重现它。没有明确的问题陈述的问题对其他读者没有用处。见:How to create a Minimal, Complete, and Verifiable example.
  • "知道这是否真的有效吗?"我认为在我们对这两个规范都有一个符合要求的实现之前,我们无法知道。
  • 我不相信你在尝试扩展 HTMLElement 时会有很多运气。
  • @torazaburo 它实际上有可能我可以用 ecma5 做到这一点,所以它应该可以用 ecma6 来完成,除非我遗漏了什么,我不知道上述语法中 traceur 不支持的任何内容,问题是没有完全没有错误
  • 检查 DOM 时看到了什么?

标签: javascript web-component ecmascript-6 traceur


【解决方案1】:

Traceur 的内联处理器似乎不支持在&lt;link import&gt; 中查找&lt;script&gt; 标记。 Traceur 的所有代码似乎都直接访问了document,这导致traceur 只查看index.html,而在x-item.html 中看不到任何&lt;scripts&gt;。这是适用于 Chrome 的解决方法。将 x-item.html 更改为:

<template id="itemtemplate">
    <span>test</span>
</template>

<script type="module">
(function() {
    let owner = document.currentScript.ownerDocument;

    class Item extends HTMLElement {
        constructor() {
            // At the point where the constructor is executed, the code
            // is not inside a <script> tag, which results in currentScript
            // being undefined. Define owner above at compile time.
            //let owner = document.currentScript.ownerDocument;
            let template = owner.querySelector("#itemtemplate");
            let clone = template.content.cloneNode(true);
            let root = this.createShadowRoot();
            root.appendChild(clone);
        }
    }
    Item.prototype.createdCallback = Item.prototype.constructor;
    Item = document.registerElement('x-item', Item);
})();
</script>

<script>
// Boilerplate to get traceur to compile the ECMA6 scripts in this include.
// May be a better way to do this. Code based on:
//   new traceur.WebPageTranscoder().selectAndProcessScripts
// We can't use that method as it accesses 'document' which gives the parent
// document, not this include document.
(function processInclude() {
    var doc = document.currentScript.ownerDocument,
        transcoder = new traceur.WebPageTranscoder(doc.URL),
        selector = 'script[type="module"],script[type="text/traceur"]',
        scripts = doc.querySelectorAll(selector);

    if (scripts.length) {
        transcoder.addFilesFromScriptElements(scripts, function() {
            console.log("done processing");
        });
    }
})();
</script>

另一种可能的解决方案是将 ECMA6 预编译为 ECMA5 并仅包含 ECMA5。这将避免 traceur 在导入中找不到 &lt;script&gt; 标记的问题,并且将不再需要样板。

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 2019-12-14
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多