干货|angular ng-content在paletx2.0模式开发中的应用

来源:互联网 发布:淘宝无法拍照怎么设置 编辑:程序博客网 时间:2024/06/05 06:41

点击上方“中兴开发者社区”,关注我们

每天读一篇一线开发者原创好文

在开发一个前端程序时,肯定会有很多相似的逻辑或布局,这时就需要提取公共组件或样式来尽可的复用代码。在angular中,提供了ng-content来更方便的编写组件和布

ng-content

  • 在组件中嵌入内容

  • 在组件中嵌入模板代码

  • select属性支持css选择器(”#id”,”.class”等等)

使用

下面以表单中常见的布局为例:

  1. <form-item>

  2.    <form-item-label>

  3.        手机号码

  4.    </form-item-label>

  5.    <form-item-wrap>

  6.        <input  name="phone" [(ngModel)]="phone"  placeholder="请输入手机号"/>

  7.    </form-item-wrap>

  8. </form-item>

我们封装了三个组件:form-item form-item-label form-item-wrap
我们期望form-item-label中的内容展示页面文字说明部分,form-item-wrap展示页面用户输入部分。

  1. <form-item>

  2.    <form-item-label>

  3.    <!-- your html code -->

  4.    </form-item-label>

  5.    <form-item-wrap>

  6.    <!-- your html code -->

  7.    </form-item-wrap>

  8. </form-item>

form-item组件代码:

  1. import { Component, ContentChild } from '@angular/core';

  2. import { FormItemLabelComponent } from './form-item-lable.component';

  3. import { FormItemWrapComponent } from './form-item-wrap.component';

  4. @Component({

  5.  selector: 'form-item',

  6.  templateUrl: './form-item.component.html'

  7. })

  8. export class FormItemComponent {

  9.  @ContentChild(FormItemLabelComponent) itemLabel;

  10.  @ContentChild(FormItemWrapComponent) itemWrap;

  11. }

  12. html:

  13. <div>

  14.    <div class="css实现">

  15.        <ng-content select="form-item-label" *ngIf="itemLabel"></ng-content>

  16.    </div>

  17.    <div class="css实现">

  18.      <ng-content select="form-item-wrap" *ngIf="itemWrap"></ng-content>

  19.    </div>

  20. </div>

form-item-label组件代码:

  1. import { Component } from '@angular/core';

  2. @Component({

  3.  selector: 'form-item-label',

  4.  template: '<ng-content></ng-content>'

  5. })

  6. export class FormItemLabelComponent {

  7. }

form-item-wrap组件代码:

  1. import { Component } from '@angular/core';

  2. @Component({

  3.  selector: 'form-item-wrap',

  4.  template: '<ng-content></ng-content>'

  5. })

  6. export class FormItemWrapComponent {

  7. }

在组件form-item中通过ng-content的select属性(使用的是css的element选择器)投影form-item-label和form-item-wrap组件内容。

在form-item-label和form-item-wrap中则使用ng-content来嵌入模板代码,如果没有ng-content则组件中的模板代码无法展示。

(组件内css样式不再展示)

ContentChild

  • ContentChild 用来从通过 Content Projection 方式 (ng-content) 设置的视图中获取匹配的元素

  • 在父组件的 ngAfterContentInit 生命周期钩子中才能成功获取通过 ContentChild 查询的元素

它和viewChild是不同的

  • ViewChild 用来从模板视图中获取匹配的元素

  • 在父组件的 ngAfterViewInit 生命周期钩子中才能成功获取通过 ViewChild 查询的元素

与css直接布局对比优势

本文中注册页面视效是左右布局,有些ux设计可能是上下布局,可能是点击操作弹出文字说明布局等等,这些布局抽象成建模分为两部分:文字说明部分和用户操作部分。使用ng-content编写组件,实际上相当于页面领域的建模,form-item-label是文字描述部分,form-item-wrap是用户操作部分,不论ux ui的设计如何,但是建模是一定的。

组件化的优势

  1. 牺牲我一个,幸福千万家

  2. 以表单举例:设计师要求左边部分带必选符号”*“或者”:”,如果是各个业务的开发童鞋来写,后期设计一旦变更,就会存在更换不及时,整个产品用户体验不一致的情况,而如果通过form-item组件内部搞定这件事情,只需要开发这个组件的同事更新组件即可,保证了整体的一致性

  3. 项目团队有一个css高手保证组件样式即可,极大的提升开发效率

总结

ng-content是组件化的神器,用好ng-content对一个项目有极大的便利


拓展阅读

input type=file 上传文件样式美化

angular2 JIT and AOT

浅谈angular2中的依赖注入

原创粉丝点击