我已经玩了一段时间的Angular Elements。我们的团队正在构建多个独立的角度项目,并将它们转换为角度元素。我们还构建了一个主壳,这些角度元素将在主壳中调用。
主壳由 Angular 编写。到目前为止,我发现我只能将角度元素嵌入主外壳中的index.html
中。但是,我想知道有没有办法在app.component.html
中使用角度元素?
index.html in mainShell:在这里使用 app1-element
工作正常。
<!DOCTYPE html>
<html lang="en">
<head>
...
<script src="../../elements/app1-element.js"></script>
</head>
<body>
<app1-element></app1-element>
</body>
</html>
角度项目结构如下所示。那么如何在app.component.html
中使用app1-element
呢?我在那里使用类似的代码,但它不起作用。顺便说一下,要运行主外壳,我使用了 mainShell/src
中的Go Live
app1
--src
----app
------app.module.ts (customElements.define('app1-element', el);)
--package.json
mainShell
--src
----app
------app.component.html (I want to use 'app1-element' here)
----index.html
--package.json
提前感谢!
我想通了。要在app.component.html
中使用 Angular 元素,您需要正确导入.js
文件。有几种方法可以做到这一点,你可以使用 SystemJS、dimport 或使用 JQuery 进行静态导入。我我的演示,我导入.js
文件静态,请参考这篇文章