英文:
How to make mat-sidenav-container fullscreen on condition?
问题
I want to make my mat-sidenav-container full screen when user is not logged in.
Trying to do this:
<mat-sidenav-container class="sidenav-container" [fullscreen]="!loggedIn">
<mat-sidenav #sidenav [class.mat-elevation-z4]="true" mode="side" class="sidenav" [fixedInViewport]="true"
[fixedTopGap]="0" [fixedBottomGap]="0" opened>
<app-side-menu></app-side-menu>
</mat-sidenav>
<mat-sidenav-content>
<div class="content-container">
<router-outlet></router-outlet>
</div>
</mat-sidenav-content>
</mat-sidenav-container>
但是编译时出错:
错误:src/app/app.component.html:28:52 - error NG8002: 无法绑定到 'fullscreen',因为它不是 'mat-sidenav-container' 的已知属性。
- 如果 'mat-sidenav-container' 是一个 Angular 组件,并且它有 'fullscreen' 输入属性,请验证它是否在此模块中。
- 如果 'mat-sidenav-container' 是一个 Web 组件,则将 'CUSTOM_ELEMENTS_SCHEMA' 添加到此组件的 '@NgModule.schemas' 以抑制此消息。
- 要允许任何属性,请将 'NO_ERRORS_SCHEMA' 添加到此组件的 '@NgModule.schemas'。
28 <mat-sidenav-container class="sidenav-container" [fullscreen]="!loggedIn">
~~~~~~~~~~~~~~~~~~~~~~~~
src/app/app.component.ts:11:16
11 templateUrl: './app.component.html',
~~~~~~~~~~~~~~~~~~~~~~
错误出现在 AppComponent 组件的模板中。
✖ 编译失败。
英文:
I want to make my mat-sidenav-container full screen when user is not logged in
Trying to do this :
<mat-sidenav-container class="sidenav-container" [fullscreen]="!loggedIn">
<mat-sidenav #sidenav [class.mat-elevation-z4]="true" mode="side" class="sidenav" [fixedInViewport]="true"
[fixedTopGap]="0" [fixedBottomGap]="0" opened>
<app-side-menu></app-side-menu>
</mat-sidenav>
<mat-sidenav-content>
<div class="content-container">
<router-outlet></router-outlet>
</div>
</mat-sidenav-content>
</mat-sidenav-container>
but got error on compilation :
Error: src/app/app.component.html:28:52 - error NG8002: Can't bind to 'fullscreen' since it isn't a known property of 'mat-sidenav-container'.
- If 'mat-sidenav-container' is an Angular component and it has 'fullscreen' input, then verify that it is part of this module.
- If 'mat-sidenav-container' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
- To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.
28 <mat-sidenav-container class="sidenav-container" [fullscreen]="!loggedIn">
~~~~~~~~~~~~~~~~~~~~~~~~
src/app/app.component.ts:11:16
11 templateUrl: './app.component.html',
~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component AppComponent.
✖ Failed to compile.
答案1
得分: 0
错误消息是预期的,因为该组件上没有全屏属性 - 请参阅 API 文档:mat-sidenav 组件。
但是,您可以通过在使用 ngClass 时有条件地添加类来实现此目标。
例如:
<mat-sidenav-container class="sidenav-container">
<mat-sidenav #sidenav [class.mat-elevation-z4]="true" mode="side" [ngClass]="{'fullscreen-nav': loggedIn === true}" [fixedInViewport]="true"
[fixedTopGap]="0" [fixedBottomGap]="0" opened>
<app-side-menu></app-side-menu>
</mat-sidenav>
<mat-sidenav-content>
<div class="content-container">
<router-outlet></router-outlet>
</div>
</mat-sidenav-content>
</mat-sidenav-container>
.fullscreen-nav {
width: 100vw !important;
height: 100vh !important;
}
还请参阅 ngClass 文档。
英文:
The error message is expected as there is no fullscreen property on the component - see api documentation: mat-sidenav component.
You can, however, achieve this by conditionally adding a class using ngClass.
Ex.
// component.html
<mat-sidenav-container class="sidenav-container">
<mat-sidenav #sidenav [class.mat-elevation-z4]="true" mode="side" [ngClass]="{'fullscreen-nav': loggedIn === true}" [fixedInViewport]="true"
[fixedTopGap]="0" [fixedBottomGap]="0" opened>
<app-side-menu></app-side-menu>
</mat-sidenav>
<mat-sidenav-content>
<div class="content-container">
<router-outlet></router-outlet>
</div>
</mat-sidenav-content>
</mat-sidenav-container>
// component.scss
.fullscreen-nav{
width: 100vw !important;
height: 100vh !important;
}
See also ngClass documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论