作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个被其他组件引用的 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/
我是一名优秀的程序员,十分优秀!