angular 居中_Angular 组件库 NG-NEST 源码解析:Drawer 组件

前言

NG-NEST介绍

今天我们来看一个弹出层组件 Drawer 抽屉 是怎么实现的。主要说明 NG-NEST 中是如何使用 @angular/cdk 中的 Overlay 来做覆盖层的。

6a0bb4f7d1839ff024605cb8cd0a881e.gif

功能分析

  • 从上下左右四个方位滑出抽屉
  • 抽屉中的内容自定义
  • 嵌套多个抽屉

代码实现

先看模板文件 lib/ng-nest/ui/drawer/drawer.component.html :

<ng-template #drawerTpl>
  <div class="x-drawer" [ngClass]="classMap" [@x-slide-animation]="placement">
    <div class="x-drawer-header" *ngIf="title">
      <div class="x-drawer-title">
        <ng-container *xOutlet="title">{
    { title }}</ng-container>
      </div>
      <x-button class="x-drawer-close" icon="fto-x" (click)="closePortal()" onlyIcon closable></x-button>
    </div>
    <div class="x-drawer-content">
      <ng-content></ng-content>
    </div>
  </div>
</ng-template>
  • 整个就是一个 ng-template 的内容模板,当我们滑出抽屉的时候渲染
  • @x-slide-animation 定义四个方位的滑出/收起动画
  • *xOutlet 这个是一个自定义指令,使用方法与 ngTemplateOutlet 一样,除了能加载模板外,还能直接设置成文字内容
  • ng-content 里面就直接映射我们写在 x-drawer 里面的内容

接下来看对应的 ts 文件:

@Component({
    
  selector: `${
    XDrawerPrefix}`,
  templateUrl: './drawer.component.html',
  styleUrls: ['./drawer.component.scss'],
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
  // 指定动画
  animations: [XSlideAnimation]
})
export class XDrawerComponent extends XDrawerProperty implements OnInit, OnChanges {
    
  // 获取整个模板
  @Vi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值