在 Angular 中创建可重用的“web-component”/app-component

彼得

我们有一个使用 angular-cli 构建的 Angular 4 应用程序。index.html我们希望将其作为插件/小部件分发,而不是使用自动生成的,客户可以将其导入到他们的页面上。类似disqus评论工作,...

使用 AngularJS 内置的先前版本,客户只需包含一个 CSS 和 JS 文件以及应用程序标签。

此问题类似于在静态和非 SPA Web 应用程序中使用 Angular 2 组件但是实际上并没有描述有效的解决方案。

这是我到目前为止所拥有的:

  • 定义deployUrl.angular-cli.json
  • 提取要包含在新 html 文件中的 js + css 文件列表
  • 像在自动生成的文件中一样,在 html 文件中添加文件 index.html

问题是我得到了https://github.com/angular/angular-cli/issues/2211 中Uncaught ReferenceError: webpackJsonp is not defined描述的

我当然可以通过 iframe 包含它,但这会使集成和自定义样式变得更加困难。

那么采用 Angular 应用程序并以某种方式使其可在其他 HTML 页面上重用的最佳选择是什么?

博格丹奇

那么采用 Angular 应用程序并以某种方式使其可在其他 HTML 页面上重用的最佳选择是什么?

我猜你想问如何开发一个 angular 组件(小部件),然后在另一个 angular 应用程序上重用它。Angular 应用程序是一个自行运行的应用程序,它不能嵌入到 HTML 页面中。

我建议您使用 @angular/cli 生成 angular 应用程序,您将仅使用它来开发您的小部件。在其中,您创建了一个将成为您的小部件的组件。您在内部创建一个模块,您可以在其中绑定与您的组件(其他组件、指令、服务等)相关的所有内容。在您生成的应用程序中,您在 app.module 上引用该模块并在 app.component.html 上将其实例化以进行开发。但这是您在组件之外所做的唯一事情。完成后,您只需将您的组件(在他的文件夹中的内容)复制到您的新应用程序并将组件模块导入到新的 app.module 中。这样您就可以根据需要获得可重用的组件/小部件。

PS:不要忘记将组件上使用的任何依赖项也引用到新应用程序中(最好的方法是尽可能地限制外部依赖项,这样您就可以拥有一个开箱即用的可重用组件)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Angular中是否可以将我的<app-custom-component> </ app-custom-component>设为html标签?

来自分类Dev

来自 app.component.ts 的 Angular 路由

来自分类Dev

是否可以在Angular 2中将@Input值传递给app.component?

来自分类Dev

从angular -oidc-client中的app.component获取数据时,this.user是未定义的

来自分类Dev

Angular2 app.component从页面中删除所有内容

来自分类Dev

Angular 2:例外:app.component.html:1:108 中的错误由:太多递归引起

来自分类Dev

在 Angular 7 中获取错误 ./src/app/app.component.ts 模块未找到:错误:无法解析

来自分类Dev

Angular app.component.html不呈现新组件

来自分类Dev

在 angular 7 中有多个 app.component

来自分类Dev

如何让 Angular 再次读取 app.component.html

来自分类Dev

如何将事件从app.component广播到angular2中的路由器出口

来自分类Dev

Angular2 将存储在变量中的数据从 app.component.ts(父组件)传送到其他组件

来自分类Dev

Angular ngx-editor 从其他 components.ts 文件中的 app.component.ts 外包配置

来自分类Dev

Angular 2.0。差异@View @Component

来自分类Dev

Angular 1.5-Component()方法

来自分类Dev

在react中如何使用可重用组件作为React Component传递给react-router-dom Component属性?

来自分类Dev

在react中如何使用可重用组件作为React Component传递给react-router-dom Component属性?

来自分类Dev

引导vs @component中的Angular2提供程序

来自分类Dev

从 Angular Component 中的 Json Response 中提取数据

来自分类Dev

Angular 在 component.ts 中存储嵌套数组

来自分类Dev

带有Webpack生产的Angular2:“无法加载app / app.component.html”

来自分类Dev

升级 app.component.ts @angular/router 和 nativescript-angular/router 的问题

来自分类Dev

导出默认类App中的钩子扩展了React.Component

来自分类Dev

动态更改app.component中的HTML元素

来自分类Dev

消耗app.component中核心模块的服务

来自分类Dev

在我的 app.component.ts 中添加 2 个组件

来自分类Dev

app.component.ts 和页面中的离子组件

来自分类Dev

在Angular 2中,通过ComponentFactory创建Component似乎无法完全呈现

来自分类Dev

如何保持一个偏离app.component模板的Angular 2视图(登录)

Related 相关文章

  1. 1

    在Angular中是否可以将我的<app-custom-component> </ app-custom-component>设为html标签?

  2. 2

    来自 app.component.ts 的 Angular 路由

  3. 3

    是否可以在Angular 2中将@Input值传递给app.component?

  4. 4

    从angular -oidc-client中的app.component获取数据时,this.user是未定义的

  5. 5

    Angular2 app.component从页面中删除所有内容

  6. 6

    Angular 2:例外:app.component.html:1:108 中的错误由:太多递归引起

  7. 7

    在 Angular 7 中获取错误 ./src/app/app.component.ts 模块未找到:错误:无法解析

  8. 8

    Angular app.component.html不呈现新组件

  9. 9

    在 angular 7 中有多个 app.component

  10. 10

    如何让 Angular 再次读取 app.component.html

  11. 11

    如何将事件从app.component广播到angular2中的路由器出口

  12. 12

    Angular2 将存储在变量中的数据从 app.component.ts(父组件)传送到其他组件

  13. 13

    Angular ngx-editor 从其他 components.ts 文件中的 app.component.ts 外包配置

  14. 14

    Angular 2.0。差异@View @Component

  15. 15

    Angular 1.5-Component()方法

  16. 16

    在react中如何使用可重用组件作为React Component传递给react-router-dom Component属性?

  17. 17

    在react中如何使用可重用组件作为React Component传递给react-router-dom Component属性?

  18. 18

    引导vs @component中的Angular2提供程序

  19. 19

    从 Angular Component 中的 Json Response 中提取数据

  20. 20

    Angular 在 component.ts 中存储嵌套数组

  21. 21

    带有Webpack生产的Angular2:“无法加载app / app.component.html”

  22. 22

    升级 app.component.ts @angular/router 和 nativescript-angular/router 的问题

  23. 23

    导出默认类App中的钩子扩展了React.Component

  24. 24

    动态更改app.component中的HTML元素

  25. 25

    消耗app.component中核心模块的服务

  26. 26

    在我的 app.component.ts 中添加 2 个组件

  27. 27

    app.component.ts 和页面中的离子组件

  28. 28

    在Angular 2中,通过ComponentFactory创建Component似乎无法完全呈现

  29. 29

    如何保持一个偏离app.component模板的Angular 2视图(登录)

热门标签

归档