3

I have a Polymer component that's referenced by other components. Something like:

In index.html

<link rel="import" href="lib/polymer/polymer.html">
<link rel="import" href="component-one.html">

...

<component-one></component-one>

In component-one.html

<link rel="import" href="sub-component.html">
<dom-module id="component-one">
    <template>
        <sub-component></sub-component>
    </template>
    <script>Polymer({ is: 'component-one' });</script>
</dom-module>

In component-two.html

<link rel="import" href="sub-component.html">
<dom-module id="component-two">
    <template>
        <sub-component></sub-component>
    </template>
    <script>Polymer({ is: 'component-two' });</script>
</dom-module>

In sub-component.html

<dom-module id="sub-component">
    <template>blah blah blah</template>
    <script>Polymer({ is: 'sub-component' });</script>
</dom-module>

The problem comes in when I attempt to dynamically load the second component in index.html:

function importHref(href) {
    return new Promise((resolve, reject) => {
        Polymer.Base.importHref(href, function (e) {
            resolve(e.target);
        }, reject, true);
    });
}

...

await importHref('component-two.html');
// Now I can use <component-two>

This throws an exception:

Uncaught DOMException: Failed to execute 'registerElement' on 'Document': Registration failed for type 'sub-component'. A type with that name is already registered.

I figure this is happening due to sub-component.html being referenced by two components, but both also reference lots of paper and iron elements and none of them cause this error.

How to I avoid this exception?

4

1 回答 1

2

问题是子组件路径中的拼写错误。

component-one.html

<link rel="import" href="../components//sub-component.html">

component-two.html

<link rel="import" href="../components/sub-component.html">

这两个都成功地路由(在 IIS/Kestrel 中)和 return sub-component.html,但被 Polymer 视为两个不同的 URI,因此是两个不同的组件。

如果您收到此错误,请仔细检查您的所有导入是否解析为相同的 URI,而不仅仅是它们返回正确的内容。

于 2017-02-20T16:39:45.757 回答