简体   繁体   English

Angular 元素:DOMException:无法在“CustomElementRegistry”上执行“define”

[英]Angular Element: DOMException: Failed to execute 'define' on 'CustomElementRegistry'

I've created two custom elements using Angular Elements.我使用 Angular Elements 创建了两个自定义元素。

<capp-customtag1> is defined in customtag1.js 
<capp-customtag2> is defined in customtag2.js.

I load <capp-customtag1> with <script type="text/javascript" src="assets/customtag1.js"></script>. 

Similarly, for <capp-customtag2>

Separately, they work as intended.分别地,它们按预期工作。 However, if I try to use both of them in the same project (an Angular 6 project), when I attempt to load the second script, I get the following error:但是,如果我尝试在同一个项目(一个 Angular 6 项目)中使用它们,当我尝试加载第二个脚本时,我会收到以下错误:

ERROR DOMException: Failed to execute 'define' on 'CustomElementRegistry': this name has already been used with this registry.错误 DOMException:无法在“CustomElementRegistry”上执行“define”:此名称已与此注册表一起使用。

The calls to CustomElementRegistry are made in customtag1.js and customtag2.js.对 CustomElementRegistry 的调用是在 customtag1.js 和 customtag2.js 中进行的。

This is the code I use to create capp-customtag1 in the Angular Element AppModule constructor:这是我用来在 Angular Element AppModule 构造函数中创建 capp-customtag1 的代码:

const el = createCustomElement(CustomTag1Component, {injector: this.injector});
customElements.define('capp-customtag1', el);

This is the code to create capp-customtag2 in the second project's AppModule constructor:这是在第二个项目的 AppModule 构造函数中创建 capp-customtag2 的代码:

const el = createCustomElement(CustomTag2Component, {injector: this.injector});
customElements.define('capp-customtag2', el);

Why do both elements have the same custom element name?为什么两个元素具有相同的自定义元素名称? And, how can I fix the problem.而且,我该如何解决这个问题。

Thank you.谢谢你。

As suspected, it's a bundling issue.正如所怀疑的那样,这是一个捆绑问题。 The root cause is that webpack (which drives the CLI) uses a runtime: webpackJsonp global, and you're overwriting that each time you load another bundle (which also defines webpackJsonp) - See webpack/webpack#3791 (comment).根本原因是 webpack(驱动 CLI)使用运行时: webpackJsonp全局,并且每次加载另一个包(也定义了 webpackJsonp)时都会覆盖它 - 请参阅webpack/webpack#3791 (评论)。 The CLI doesn't expose this option (things like this are why angular are not supporting this use case yet). CLI 没有公开这个选项(这就是为什么 angular 还不支持这个用例)。 You could (though I don't recommend it) manually rename that global webpackJsonp in each bundle to something unique.您可以(尽管我不推荐)手动将每个包中的全局webpackJsonp重命名为唯一的名称。

You're also duplicating all the polyfills , which is likely to cause all kinds of unexpected results, as they're shimming native APIs and overwriting them at various times.您还复制了所有polyfills ,这可能会导致各种意外结果,因为它们正在填充本机 API 并在不同时间覆盖它们。 Further, bundling a copy of all the angular packages into each bundle seems suboptimal.此外,将所有 angular 包的副本捆绑到每个包中似乎不是最理想的。

For the moment, if you want to do this sort of use case, your likely best option is going to be to use something like rollup to build UMD bundles , that rely on angular's UMD bundles and exclude the angular source from each element's package.目前,如果你想做这种用例,你可能最好的选择是使用 rollup 之类的东西来build UMD bundles ,它依赖于 angular 的 UMD bundles 并从每个元素的包中排除 angular 源。 It's going to take some manual work.这将需要一些手动工作。

Alternately, don't use the CLI to build the individual elements into binaries, treat them as libraries and bring them into the build properly, so you only have one webpack runtime.或者,不要使用 CLI 将单个元素构建为二进制文件,将它们视为库并将它们正确地引入构建中,因此您只有一个 webpack 运行时。

I kept running into this error and I added a check to make sure the customElement was not already defined.我一直遇到这个错误,并添加了一个检查以确保 customElement 尚未定义。 Simply adding the following before defining the element should fix this error:只需在定义元素之前添加以下内容即可修复此错误:

if (!customElements.get('webtest')) {  
    customElements.define('webtest', e3);
}

I know this is quite late to answer but I faced the issue last night and these are my observations.我知道现在回答已经很晚了,但我昨晚遇到了这个问题,这些是我的观察。 Hope it helps others with this problem ahead.希望它可以帮助其他人解决这个问题。

The error is because of webpack conflict between the various angular applications on your page.错误是因为页面上各种 angular 应用程序之间的 webpack 冲突。

The solution is to change the jsonpFunction name in webpack.config.js of each of your web component applications.解决方案是更改每个 Web 组件应用程序的webpack.config.js中的jsonpFunction名称。

Example:例子:

module.exports = {
  //...
  output: {
    jsonpFunction: '<unique name for your application's json function>'
  }
};

Do this for both of your projects related to capp-customtag1 and capp-customtag1 .对与capp-customtag1capp-customtag1相关的两个项目执行此capp-customtag1

I am facing similar kind of issue, what I have done is I put a hyphen in between the selector name.我面临类似的问题,我所做的是在选择器名称之间放了一个连字符。

my element selector name webtest to web-test我的元素选择器名称webtestweb-test

in app.module.js在 app.module.js 中

before

const e3 = createCustomElement(WebelementTestComponent, { injector: this.injector });
customElements.define('webtest', e3);

after

const e3 = createCustomElement(WebelementTestComponent, { injector: this.injector });
customElements.define('web-test', e3);

then my problem has been solved.那么我的问题就解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 6 - ERROR DOMException:无法在&#39;Element&#39;上执行&#39;setAttribute&#39;: - Angular 6 - ERROR DOMException: Failed to execute 'setAttribute' on 'Element': DOMException:无法在“元素”上执行“setAttribute”:“[routerLink”不是有效的属性名称 - DOMException: Failed to execute 'setAttribute' on 'Element': '[routerLink' is not a valid attribute name DOMException:无法在“IDBObjectStore”上执行“getAll”:事务在 angular 中不活动 - DOMException: Failed to execute 'getAll' on 'IDBObjectStore': The transaction is not active in angular 错误 DOMException:无法在“元素”上执行“setAttribute”:“{{”不是有效的属性名称 - ERROR DOMException: Failed to execute 'setAttribute' on 'Element': '{{' is not a valid attribute name 未捕获的 DOMException:无法在“WorkerGlobalScope”上执行“importScripts” - Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope' 错误 DOMException:无法在“元素”上执行“setAttribute”:“novalidate(ngSubmit)”不是有效的属性名称 - ERROR DOMException: Failed to execute 'setAttribute' on 'Element': 'novalidate(ngSubmit)' is not a valid attribute name 错误 DOMException:无法在“文档”上执行“querySelector” - ERROR DOMException: Failed to execute 'querySelector' on 'Document' 未捕获的DOMException:无法在'Window'上执行'postMessage' - Uncaught DOMException: Failed to execute 'postMessage' on 'Window' 无法在“元素”上执行“setAttribute”:Angular 11 - Failed to execute 'setAttribute' on 'Element': Angular 11 如何修复DOMException:无法在&#39;XMLHttpRequest&#39;上执行&#39;open&#39;:调用第三方服务器/ api时无效的URL Angular HttpClient? - How to fix DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL Angular HttpClient when calling a third party server/api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM