我有这个组件StackBliz
模板:
<mat-sidenav-container class="sidenav-container">
<mat-sidenav #drawer class="sidenav" fixedInViewport
[attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
[mode]="(isHandset$ | async) ? 'over' : 'side'"
[opened]="(isHandset$ | async) === false">
<mat-toolbar>Menu</mat-toolbar>
<mat-nav-list>
<a mat-list-item href="#">Link 1</a>
<a mat-list-item href="#">Link 2</a>
<a mat-list-item href="#">Link 3</a>
</mat-nav-list>
</mat-sidenav>
<mat-sidenav-content>
<mat-toolbar color="primary">
<button
type="button"
aria-label="Toggle sidenav"
mat-icon-button
(click)="drawer.toggle()"
*ngIf="isHandset$ | async">
<mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
</button>
<span>dashboard-app</span>
</mat-toolbar>
</mat-sidenav-content>
</mat-sidenav-container>
班级:
export class NavigationComponent {
isHandset$: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.Handset)
.pipe(
map(result => result.matches),
shareReplay()
);
constructor(private breakpointObserver: BreakpointObserver) {}
}
我想减少页面上的订阅数量。
我尝试将内容包装到 中ng-container
,但这没有用。
首先,我在模板中遇到错误[opened]="isHandset === false"
,其次是ng-container
制动视图,所以即使我离开,也[opened]="(isHandset$ | async) === false"
没有显示任何内容
<mat-sidenav-container class="sidenav-container">
<ng-container *ngIf="isHandset$ | async as isHadset">
<mat-sidenav #drawer class="sidenav" fixedInViewport
[attr.role]="isHadset ? 'dialog' : 'navigation'"
[mode]="isHadset ? 'over' : 'side'"
[opened]="isHadset === false"> //got an error here saying this condition is always false because isHadset is always 'true'
...
*ngIf="isHadset"
...
</mat-sidenav-content>
<ng-container>
</mat-sidenav-container>
任何想法如何解决我上面描述的所有两个问题?