The goal is to create a web components using angular and use it in an external html file.

我创建了Web组件:‘my-web-Component’

(async () => {
  const app: ApplicationRef = await createApplication(appConfig);

  // Define Web Components
  const MyComponent = createCustomElement(MyComponentComponent, { injector: app.injector });
  customElements.define('my-web-component', MyComponent);
})();

我发球了(NG发球),定制元素起作用了! 因此,我构建我的项目时获得了3个文件:main.js、Polyfis.js和index.html(/dist). 我用Live Server测试了dist/index.html,但Custom元素仍然有效.

<!doctype html>
<html lang="en" data-critters-container>
<head>
  <meta charset="utf-8">
  <title>WebComponentClean</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles-5INURTSO.css"></head>
<body>
  <my-web-component></my-web-component>
  <script src="polyfills-RT5I6R6G.js" type="module"></script>
  <script src="main-QVCUGX7F.js" type="module"></script>
  <script src="prova.js"></script>
</body>
</html>

enter image description here

Now there is the problem个 我在这个项目中创建了第二个index2.html,但是在src文件夹之外,并且我测试了我的Web组件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/dist/web-component-clean/browser/styles-5INURTSO.css">
</head>
<body>
    <my-web-component></my-web-component>
    <script src="/dist/web-component-clean/browser/main-QVCUGX7F.js" type="module"></script>
    <script src="/dist/web-component-clean/browser/polyfills-RT5I6R6G.js" type="module"></script>
    <script src="/dist/web-component-clean/browser/prova.js"></script>
</body>
</html>

errore della console

file main.js

推荐答案

进口的顺序差别很大,如果你看第二个index.html,mainpolyfills都是互换的!反转过来!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/dist/web-component-clean/browser/styles-5INURTSO.css">
</head>
<body>
    <my-web-component></my-web-component>
    <script src="/dist/web-component-clean/browser/polyfills-RT5I6R6G.js" type="module"></script>
    <script src="/dist/web-component-clean/browser/main-QVCUGX7F.js" type="module"></script>
    <script src="/dist/web-component-clean/browser/prova.js"></script>
</body>
</html>

Angular相关问答推荐

Angular:浏览器中未显示一些Google Content图标

to Signal on Observables inside Signal

如何防止打印后的空格后的HTML元素的Angular ?

如何防止变量值重置?

FromEvent不工作,本机事件绑定

出错后可观察到的重用

当try 关闭浏览器选项卡时显示alert 时,我是否可以捕获用户是否取消警告

具有两个订阅方法的 Angular 16 takeUntilDestroyed 运算符

如何在Angular中自动路由到子路由

显示 1 个数据而不是所有 ngFor - Angular 11

如何以Angular 改变环境

过滤 ngfor Angular 的计数

如何为所有模块全局声明指令?

如何在两个模块之间共享服务 - @NgModule 在Angular 而不是在组件之间?

如何在Angular 4中为数字管道指定语言环境千位分隔符

使用 DomSanitizer 绕过安全性后,安全值必须使用 [property]=binding

提交后在Angular 2中重置表单

如何在 Angular 2 中链接 HTTP 调用?

Angular2动态改变CSS属性

Angular2/4 中的ng-reflect-*属性有什么作用?