gpt4 book ai didi

angular - 在移动设备上显示 Accordion 并在其他设备上显示标签

转载 作者:行者123 更新时间:2023-12-03 20:54:07 27 4
gpt4 key购买 nike

我的应用程序中有多个页面使用 ngx-tabs (https://valor-software.com/ngx-bootstrap/#/tabs),这些标签在移动设备上不太适合,所以在移动设备上,我想显示 ngx-accordion (https://valor-software.com/ngx-bootstrap/#/accordion) 而不是标签。我可以使用 Angular 断点观察器来实现此功能,但对于特定页面。我需要跨应用程序使用它,并试图弄清楚如何编写可重用的自定义指令或通用组件。

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>基于断点。

谢谢!

最佳答案

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

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

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>

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

现在要获得使用这些指令的 Accordion /标签集的特定组件包装器......我们需要另一个指令和一个组件(注意,我正在使用 Material 标签/ 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 标记以允许模板注入(inject)。问题是我们不能像往常一样在这里使用 ng-content,因为我们将内容投影到不同的组件中,所以我们需要解决这个问题。有点困惑,但仍然有效。

在屏幕尺寸发生变化时,您可能需要某种方法来协调选定的选项卡/展开的 Accordion 元素,但这将是非常特定于 lib 的,如果您唯一关心的是区分移动设备和 web,则可能不需要。

这种方法的最大好处是您可以在任何需要它们的地方使用结构指令,并且您不仅限于制表符或 Accordion 。

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

关于angular - 在移动设备上显示 Accordion 并在其他设备上显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61495176/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com