聚合物错误 "A type with that name is already registered" 使用 Polymer.Base.importHref

Polymer error "A type with that name is already registered" using Polymer.Base.importHref

我有一个被其他组件引用的 Polymer 组件。类似于:

index.html

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

...

<component-one></component-one>

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>

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>

sub-component.html

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

当我尝试在 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>

这会引发异常:

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

我认为发生这种情况是因为 sub-component.html 被两个组件引用,但它们都引用了大量纸和铁元素,其中 none 导致了此错误。

如何避免这个异常?

问题是 sub-component.

路径中的拼写错误

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,而不仅仅是它们 return 正确的内容。