我有使用Angular Material 6生成的响应式mat-sidenav
。导航面板的“打开/关闭”按钮工作出色,但随后导航面板打开时,面板的内容模糊了一部分。
如何使内容部分像橡胶一样:
<!--CONTENT-->
<main>
<router-outlet></router-outlet>
</main>
我的意思是,当导航栏关闭时,内容宽度为页面的100%。但是,当打开导航栏时,内容压缩到85%(我的导航栏有width: 15%
)
这是模板:
<mat-sidenav-container class="sidenav-container">
<mat-sidenav
mode="side"
#drawer
class="sidenav"
[(opened)]="isOpened"
fixedInViewport="true"
[attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
[mode]="(isHandset$ | async) ? 'over' : 'side'"
[opened]="(isHandset$ | async)"
>
<mat-nav-list>
<span
*ngFor="let link of links"
routerLinkActive="active"
>
<a mat-list-item [routerLink]="[link.url]">
{{link.name}}
</a>
</span>
<span style="position: absolute; bottom: 1px; width: 100%">
<a mat-list-item class="waves-effect waves-orange" (click)="logout($event)">
Выйти
</a>
</span>
</mat-nav-list>
</mat-sidenav>
<mat-sidenav-content>
<mat-toolbar color="primary">
<button mat-icon-button (click)="drawer.toggle()">
<mat-icon>menu</mat-icon>
</button>
<span style="margin-left: 45%">Oasis Bot Manager</span>
</mat-toolbar>
</mat-sidenav-content>
</mat-sidenav-container>
<!--CONTENT-->
<main [ngStyle]="{'width': isOpened ? '85%' : '100%'}">
<router-outlet></router-outlet>
</main>
TypeScript方面:
export class SiteLayoutComponent {
links = [
{url: '/vds_list', name: 'Статистика'},
{url: '/profile', name: 'Личный кабинет'}
];
isOpened;
isHandset$: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.Handset)
.pipe(
map(result => result.matches)
);
constructor(private breakpointObserver: BreakpointObserver,
private auth: AuthService,
private router: Router) {
}
logout(event: Event) {
event.preventDefault();
this.auth.logout();
this.router.navigate(['/login']);
}
}
我正在尝试通过width
将[ngStyle]
的内容绑定到[(opened)]="isOpened"
的{{1}}状态。
但是不行。如何解决这种情况?
答案 0 :(得分:1)
Your content area needs to be inside mat-sidenav-container below the toolbar:
<mat-sidenav-container class="sidenav-container">
<mat-sidenav
mode="side"
#drawer
class="sidenav"
[(opened)]="isOpened"
fixedInViewport="true"
[attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
[mode]="(isHandset$ | async) ? 'over' : 'side'"
[opened]="(isHandset$ | async)"
>
<mat-nav-list>
<span
*ngFor="let link of links"
routerLinkActive="active"
>
<a mat-list-item [routerLink]="[link.url]">
{{link.name}}
</a>
</span>
<span style="position: absolute; bottom: 1px; width: 100%">
<a mat-list-item class="waves-effect waves-orange" (click)="logout($event)">
Выйти
</a>
</span>
</mat-nav-list>
</mat-sidenav>
<mat-sidenav-content style="display:flex; flex-direction:column;">
<mat-toolbar color="primary">
<button mat-icon-button (click)="drawer.toggle()">
<mat-icon>menu</mat-icon>
</button>
<span style="margin-left: 45%">Oasis Bot Manager</span>
</mat-toolbar>
<!--CONTENT-->
<main>
<router-outlet></router-outlet>
</main>
</mat-sidenav-content>
</mat-sidenav-container>