在移动设备上显示手风琴,在其他设备上显示Tab

时间:2020-04-29 05:56:44

标签: angular typescript ngx-bootstrap ngx-bootstrap-accordion

我的应用程序中有多个页面使用ngx-tabs(https://valor-software.com/ngx-bootstrap/#/tabs),这些选项卡不适用于移动设备,因此在移动设备上,我想显示ngx-accordion(https://valor-software.com/ngx-bootstrap/#/accordion )而不是标签。我可以使用angular breakpointobserver实现此功能,但对于特定页面。我需要在整个应用程序中使用它,并试图找出如何编写可重用的自定义指令或通用组件。

abc.component.html:


    <div>
      <tabset *ngIf="tabs">
        <tab heading="Basic title" id="tab1">Basic content</tab>
        <tab heading="Basic Title 1">Basic content 1</tab>
        <tab heading="Basic Title 2">Basic content 2</tab>
      </tabset>

    <accordion *ngIf="!tabs">
      <accordion-group heading="Basic title">
            Basic content
      </accordion-group>
      <accordion-group heading="Basic title 1">
           Basic content 1
      </accordion-group>
      <accordion-group heading="Basic title 2">
           Basic content 2
      </accordion-group>
      </accordion>
    </div>

abc.component.ts


import { Component, OnInit, ElementRef } from "@angular/core";
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';

@Component({
  selector: "app-abc",
  templateUrl: "abc.component.html"
})
export class AbcComponent implements OnInit {
  tabs: boolean = true;

  constructor(private observer: BreakpointObserver) {
    observer.observe([Breakpoints.Small, Breakpoints.Handset, Breakpoints.HandsetPortrait, Breakpoints.HandsetLandscape]).subscribe((result) => {
      if (result.matches) this.tabs = false;
      else this.tabs = true;
    });
  }


  ngOnInit(): void {
  }


}

基本上我需要这样的东西

<my-tab>
  <my-tab-item heading="Basic title"> Basic content </my-tab-item>
  <my-tab-item heading="Basic title1"> Basic content 1</my-tab-item>
  <my-tab-item heading="Basic title2"> Basic content 2</my-tab-item>
</my-tab>

根据断点转换为<tab><accordion>

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以实现此目标,并通过一些指令和组件来获得高度可重用的组件。

首先,让我们创建一些结构性指令以帮助封装断点观察器逻辑,以便我们可以在需要时使用它:

import {Directive, TemplateRef, ViewContainerRef, OnDestroy} from '@angular/core';
import { BreakpointObserver, Breakpoints, BreakpointState } from '@angular/cdk/layout';
import {Subscription} from 'rxjs'

const MOBILE_STATES = [Breakpoints.HandsetLandscape,Breakpoints.HandsetPortrait];
// base directive that implements the breakpoint observer logic and renders accordingly
abstract class BreakPointObserverDirective implements OnDestroy {
  private hasView = false;
  private sub: Subscription;

  constructor(private tmp: TemplateRef<any>, private viewContainer: ViewContainerRef, private observer: BreakpointObserver, showMobile: boolean) {
    this.sub = this.observer.observe(MOBILE_STATES).subscribe(({matches}) => {
      if ((matches && showMobile) || (!matches && !showMobile)) {
        this.render()
      } else {
        this.clear()
      }
    })
  }

  render() {
    if (!this.hasView) {
      this.viewContainer.createEmbeddedView(this.tmp);
      this.hasView = true;
    }
  }

  clear() {
    if (this.hasView) {
      this.viewContainer.clear();
      this.hasView = false;
    }
  }

  ngOnDestroy() {
    this.sub.unsubscribe()
  }
}

// implementation for mobile
@Directive({
  selector: '[ifMobile]'
})
export class IfMobileDirective extends BreakPointObserverDirective {
  constructor(tmp: TemplateRef<any>, viewContainer: ViewContainerRef, observer: BreakpointObserver) {
    super(tmp, viewContainer, observer, true)
  }
}

// implementation for web
@Directive({
  selector: '[ifWeb]'
})
export class IfWebDirective extends BreakPointObserverDirective {
  constructor(tmp: TemplateRef<any>, viewContainer: ViewContainerRef, observer: BreakpointObserver) {
    super(tmp, viewContainer, observer, false)
  }
}

您可以在模板中使用以下代码:

  <tabset *ifWeb>
    <tab heading="Basic title" id="tab1">Basic content</tab>
    <tab heading="Basic Title 1">Basic content 1</tab>
    <tab heading="Basic Title 2">Basic content 2</tab>
  </tabset>

<accordion *ifMobile>
  <accordion-group heading="Basic title">
        Basic content
  </accordion-group>
  <accordion-group heading="Basic title 1">
       Basic content 1
  </accordion-group>
  <accordion-group heading="Basic title 2">
       Basic content 2
  </accordion-group>
</accordion>

基本上是您已经拥有的逻辑,但是封装在结构指令中。您可以根据需要扩展此功能/获得更多创意。

现在要获取使用这些指令的手风琴/制表集周围的特定组件包装...我们将需要另一个指令和一个组件(注意,我使用的是材质制表符/手风琴,但是任何组件库都应类似地工作,但我从未使用过您正在使用的特定库,也不知道它的实现程度如何):

import {Directive, TemplateRef, Component, ContentChildren, QueryList, Input} from '@angular/core';
// directive to find the template to render and accept input like header
// this is where you'd match the parts of the component API you need to mirror
@Directive({
  selector: '[mobileSwitchContent]'
})
export class MobileSwitchContentDirective {
  @Input() header: string;

  constructor(public tmp: TemplateRef<any>) { }
}

// component that finds content directives and implements needed template
@Component({
  selector: 'mobile-switch',
  template: `
    <mat-accordion *ifMobile>
      <mat-expansion-panel *ngFor="let c of content">
        <mat-expansion-panel-header>
          <mat-panel-title>
            {{c.header}}
          </mat-panel-title>
        </mat-expansion-panel-header>
        <ng-container *ngTemplateOutlet="c.tmp"></ng-container>
      </mat-expansion-panel>
    </mat-accordion>

    <mat-tab-group *ifWeb>
      <mat-tab *ngFor="let c of content" [label]="c.header">  
        <ng-container *ngTemplateOutlet="c.tmp"></ng-container>
      </mat-tab>
    </mat-tab-group>
  `
})
export class MobileSwitchComponent {
  @ContentChildren(MobileSwitchContentDirective)
  content: QueryList<MobileSwitchContentDirective>
}

我认为适合您的图书馆的模板应为:

<tabset *ifWeb>
  <tab *ngFor="let c of content" [heading]="c.header">
    <ng-container *ngTemplateOutlet="c.tmp"></ng-container>
  </tab>
</tabset>

<accordion *ifMobile>
  <accordion-group *ngFor="let c of content" [heading]="c.header">
    <ng-container *ngTemplateOutlet="c.tmp"></ng-container>
  </accordion-group>
</accordion>

这使得您可以很接近您的预期用途:

<mobile-switch>
  <ng-template mobileSwitchContent header="First">
    Content 1
  </ng-template>
  <ng-template mobileSwitchContent header="Second">
    Content 2
  </ng-template>
</mobile-switch>

您需要带有指令的ng-template标签以允许模板注入。问题在于我们不能像通常那样使用ng-content,因为我们将内容投影到不同的组件中,因此我们需要解决这一问题。有点混乱,但仍然有效。

您可能需要某种方法来在屏幕尺寸更改的情况下协调选定的制表符/展开的手风琴元素,但这将是非常特定于lib的,如果您唯一关心的是区分移动设备和网络,则可能不需要。

这种方法的最大好处是您可以在需要的任何地方使用结构指令,而不仅限于制表符或手风琴。

闪电战:https://stackblitz.com/edit/angular-9-material-starter?file=src%2Fapp%2Fmobile-switch.ts