简体   繁体   English

在Angular 2+中拆分组件HTML?

[英]Split component HTML in Angular 2+?

I have an Angular 5 app with lot of complex forms and layouts. 我有一个Angular 5应用程序,有很多复杂的表单和布局。 I would like to split the component HTML into several files and use a single tag to represent them. 我想将组件HTML拆分成几个文件,并使用单个标记来表示它们。 Is it possible to create a directive to replace a tag with a file's contents? 是否可以创建一个指令来用文件的内容替换标签?

Is there a better way of handling long HTML files with templates for loading, loaded and error states? 有没有更好的方法来处理带有加载,加载和错误状态模板的长HTML文件?

Imagine a feed.component.html has different templates for different types of feed items (images / links / videos / text / etc.) governed by an ngIf . 想象一下,一个feed.component.html有不同类型的由管辖饲料项目(图片/链接/视频/文本/等)不同的模板ngIf What is the ideal way of maintaining this file? 维护此文件的理想方式是什么?

Angular best practice talks about one html (and one ts file) per component. Angular最佳实践讨论了每个组件的一个html(和一个ts文件)。

If your html in very long and becomes unreadable, probably your logic has the potencial to become too heavy as well . 如果你的html很长并且变得不可读,那么你的逻辑也可能变得太重了

I would consider dividing the component into sub-components, eg: 我会考虑将组件划分为子组件,例如:

feed
  ├─ feed.component.html
  ├─ feed.component.scss
  ├─ feed.component.spec.ts
  ├─ feed.component.ts
  └ feeds
     ├─ image
     │   ├─ image.component.html
     │   :
     │   :
     ├─ link
     │   ├─ link.component.html
     │   :
     │   :

Could you make a separate component for each feed item? 你能为每个饲料项目制作一个单独的组件吗? I think that might make it easier to read and understand. 我认为这可能会使阅读和理解更容易。

Depending on what your doing, you might even be able to generate these components dynamically instead of using if blocks. 根据您的操作,您甚至可以动态生成这些组件,而不是使用if块。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM