如何根据条件使 mat-sidenav-container 全屏?

huangapple go评论52阅读模式
英文:

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' 的已知属性。

  1. 如果 'mat-sidenav-container' 是一个 Angular 组件,并且它有 'fullscreen' 输入属性,请验证它是否在此模块中。
  2. 如果 'mat-sidenav-container' 是一个 Web 组件,则将 'CUSTOM_ELEMENTS_SCHEMA' 添加到此组件的 '@NgModule.schemas' 以抑制此消息。
  3. 要允许任何属性,请将 '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 :

&lt;mat-sidenav-container class=&quot;sidenav-container&quot; [fullscreen]=&quot;!loggedIn&quot;&gt;
    &lt;mat-sidenav #sidenav [class.mat-elevation-z4]=&quot;true&quot; mode=&quot;side&quot; class=&quot;sidenav&quot; [fixedInViewport]=&quot;true&quot;
      [fixedTopGap]=&quot;0&quot; [fixedBottomGap]=&quot;0&quot; opened&gt;
      &lt;app-side-menu&gt;&lt;/app-side-menu&gt;
    &lt;/mat-sidenav&gt;
    &lt;mat-sidenav-content&gt;
      &lt;div class=&quot;content-container&quot;&gt;
        &lt;router-outlet&gt;&lt;/router-outlet&gt;
      &lt;/div&gt;
    &lt;/mat-sidenav-content&gt;
  &lt;/mat-sidenav-container&gt;

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'.

  1. If 'mat-sidenav-container' is an Angular component and it has 'fullscreen' input, then verify that it is part of this module.
  2. If 'mat-sidenav-container' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
  3. 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

&lt;mat-sidenav-container class=&quot;sidenav-container&quot;&gt;
    &lt;mat-sidenav #sidenav [class.mat-elevation-z4]=&quot;true&quot; mode=&quot;side&quot; [ngClass]=&quot;{&#39;fullscreen-nav&#39;: loggedIn === true}&quot; [fixedInViewport]=&quot;true&quot;
      [fixedTopGap]=&quot;0&quot; [fixedBottomGap]=&quot;0&quot; opened&gt;
      &lt;app-side-menu&gt;&lt;/app-side-menu&gt;
    &lt;/mat-sidenav&gt;
    &lt;mat-sidenav-content&gt;
      &lt;div class=&quot;content-container&quot;&gt;
        &lt;router-outlet&gt;&lt;/router-outlet&gt;
      &lt;/div&gt;
    &lt;/mat-sidenav-content&gt;
  &lt;/mat-sidenav-container&gt;


// component.scss

    .fullscreen-nav{
    	width: 100vw !important;
    	height: 100vh !important;
    }

See also ngClass documentation.

huangapple
  • 本文由 发表于 2023年5月15日 04:50:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定