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

Angular 在移动设备上显示手风琴,在其他设备上显示tab,angular,typescript,ngx-bootstrap,ngx-bootstrap-accordion,Angular,Typescript,Ngx Bootstrap,Ngx Bootstrap Accordion,我的应用程序中有多个页面使用ngx tabs(),这些选项卡不适合移动设备,因此在移动设备上,我想显示ngx accordion(),而不是选项卡。我可以使用角度观察者来实现这个功能,但是对于一个特定的页面。我需要在整个应用程序中使用它,并试图找出如何编写一个可重用的自定义指令或通用组件 abc.component.html: <div> <tabset *ngIf="tabs"> <tab heading="Basic tit

我的应用程序中有多个页面使用ngx tabs(),这些选项卡不适合移动设备,因此在移动设备上,我想显示ngx accordion(),而不是选项卡。我可以使用角度观察者来实现这个功能,但是对于一个特定的页面。我需要在整个应用程序中使用它,并试图找出如何编写一个可重用的自定义指令或通用组件

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>
基本上我需要这样的东西

<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>

基本内容
基本内容1
基本内容2
根据断点将其转换为


谢谢大家!

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

首先,让我们创建两个结构指令来帮助封装断点观察者逻辑,以便我们可以在需要的地方使用它:

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)
  }
}
import{Directive,TemplateRef,ViewContainerRef,OnDestroy}来自“@angular/core”;
从'@angular/cdk/layout'导入{BreakpointObserver,Breakpoints,BreakpointState};
从“rxjs”导入{Subscription}
const MOBILE_STATES=[Breakpoints.HandsetLandscape,Breakpoints.HandsetGraphic];
//实现断点观察者逻辑并相应呈现的base指令
抽象类BreakPointObserverDirective实现OnDestroy{
私有hasView=false;
私人分:认购;

构造函数(private tmp:TemplateRef

你的想法是对的?另一个实现出了什么问题?你只需要将它包装在一个组件中,然后在其他地方重用它。除非我遗漏了什么?
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>