在移动设备上显示手风琴,在其他设备上显示标签。

问题描述 投票:0回答:1

在我的应用程序中,有多个页面使用ngx-tabs(https:/valor-software.comngx-bootstrap#tabs。),标签在移动设备上不太合适,所以在移动设备上,我想显示ngx-accordion (https:/valor-software.comngx-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.html: 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> 基于一个断点。

谢谢您

angular typescript ngx-bootstrap ngx-bootstrap-accordion
1个回答
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,也许不需要,如果你唯一关心的是移动和web之间的区别。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.