gpt4 book ai didi

javascript - 使用 Polymer.Base.importHref 的 polymer 错误 "A type with that name is already registered"

转载 作者:行者123 更新时间:2023-11-29 14:38:21 25 4
gpt4 key购买 nike

我有一个被其他组件引用的 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 被两个组件引用,但它们都引用了大量的纸和铁元素,但没有一个会导致此错误。

如何避免这种异常?

最佳答案

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

component-one.html

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

component-two.html

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

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

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

关于javascript - 使用 Polymer.Base.importHref 的 polymer 错误 "A type with that name is already registered",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42348832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com