簡體   English   中英

Angular Material mat-sidenav-content 未顯示

[英]Angular Material mat-sidenav-content not showing

角度材質版本:@angular/material@14.0.2

我是第一次使用 Angular 框架構建 Web 應用程序。 app-routing-module 延遲加載一個儀表板模塊,該模塊聲明所需的組件並導入所需的模塊。 儀表板模塊導入一個儀表板路由模塊,該模塊包含以 WrapperComponent 作為父級、側導航內容作為子級的路由,分成不同的組件。

儀表板-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AboutComponent } from './components/about/about.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { LoginComponent } from './components/login/login.component';
import { WrapperComponent } from './components/wrapper/wrapper.component';

const routes: Routes = [
{
    path: '',
    component: WrapperComponent,
    children: [
        {
            path: 'dashboard', // --> localhost:4200/dashboard
            component: DashboardComponent,
        },
        {
            path: 'login', // --> localhost:4200/login
            component: LoginComponent,
        },
        {
            path: 'about', // --> localhost:4200/about
            component: AboutComponent,
        }
    ]
},
{
    path: '**',
    redirectTo: '/dashboard',
    pathMatch: 'full'
}
];

@NgModule({
     imports: [RouterModule.forChild(routes)],
     exports: [RouterModule]
})
export class DashboardRoutingModule { }

wrapper.component.html

<mat-sidenav-container>
<mat-sidenav #sideNav mode="side" opened="opened">
    <app-side-nav>
    </app-side-nav>
</mat-sidenav>
<mat-sidenav-content>
    <router-outlet></router-outlet>
</mat-sidenav-content>
</mat-sidenav-container>

app-side-nav 組件由路由器鏈接組成。

<div class="sidenav">
<div class="logo">
    <a (click)="toggleMenuState()" class="simple-text logo-mini">
        <div class="logo-img">
            <img src="./assets/images/sample_logo.png" alt="logo">
        </div>
    </a>
</div>

<ul class="nav">
    <li class="active nav-list-item"><a routerLink="/default-route"><i class="fa fa-dashboard fa-nav-icon"><span class="nav-item-text">Dashboard</span></i></a></li>
    <li class="nav-list-item"><a routerLink="/some-route"><i class="fa fa-group fa-nav-icon"><span class="nav-item-text">Groups</span></i></a></li>
    <li class="nav-list-item"><a routerLink="/some-route"><i class="fa fa-line-chart fa-nav-icon"><span class="nav-item-text">Charts</span></i></a></li>
    <li class="nav-list-item"><a routerLink="/some-route"><i class="fa fa-book fa-nav-icon"><span class="nav-item-text">Portfolio</span></i></a></li>
    <li class="nav-list-item"><a routerLink="/login"><i class="fa fa-user fa-nav-icon"><span class="nav-item-text">Login</span></i></a></li>
    <li class="nav-list-item"><a routerLink="/some-route"><i class="fa fa-gear fa-nav-icon"><span class="nav-item-text">Settings</span></i></a></li>
</ul>

這似乎工作正常,因為我可以看到根據單擊的按鈕在 DOM 中加載的相關內容。 但由於某種原因,內容不可見。 這是加載的儀表板組件的屏幕截圖。 出於某種原因,它以 1920 像素的邊距加載,但即使刪除,內容仍然不可見。 應用程序 DOM

該結構似乎有效,但有些地方不太正確,否則內容會顯示。 將不勝感激任何想法,建議或進一步的問題。

我認為您的問題很重要,認為您的 app-side-nav 或 mat-sidenav 具有全屏寬度,在我可以看到的屏幕截圖中,內容的左側邊距為 1920px,這意味着您的視圖超出了屏幕。 你的截圖 我的示例具有正確的邊距,請注意您的導航組件: 我的截圖 Stackblitz 示例希望這會有所幫助。

檢查呈現的模板后,您的代碼似乎缺乏路由精度。

您的app-routing.module.ts說對於路徑""它應該使用DashboardModule 在模塊中,您說對於路徑""它應該呈現WrapperComponent 在此之后,它沒有默認路由。

所以,基本上發生的事情是你的包裝器組件被渲染了,但是它的子組件都不匹配路由。 要修復它,您有兩種選擇:

  • 在包裝子路由中為“”路由提供處理程序
  • 就像我在您的 GitHub 項目的 Stackblitz 分支中所做的那樣,使用通配符路由向其中一個路由添加重定向。

Stackblitz 前叉

那就是修復路由器插座不渲染任何東西。

另一個問題是<mat-sidenav #sideNav mode="side" opened="opened"> ,因為您正在為布爾標志分配一個字符串,這意味着它將始終是真實的並且不會顯示下面的內容。

結果刪除opened="opened"並添加重定向到儀表板頁面,我們可以看到“儀表板工作!”

在此處輸入圖像描述

PS 查看文檔中的示例,它看起來並沒有按照您希望的方式工作。 而是顯示或隱藏行為。 不展開。

PSS 您可以嘗試按照此處的建議為永遠打開的 sidenav 手動使用自動調整大小和切換寬度

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM