简体   繁体   English

Angular Material mat-sidenav-content 未显示

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

Angular Material Version: @angular/material@14.0.2角度材质版本:@angular/material@14.0.2

I am building a web application using the Angular framework for the first time.我是第一次使用 Angular 框架构建 Web 应用程序。 The app-routing-module lazy loads a dashboard module that declares the desired components and imports the required modules. app-routing-module 延迟加载一个仪表板模块,该模块声明所需的组件并导入所需的模块。 The dashboard module imports a dashboard routing module which holds the routes with the WrapperComponent as the parent and the side nav content as the children, split up into different components.仪表板模块导入一个仪表板路由模块,该模块包含以 WrapperComponent 作为父级、侧导航内容作为子级的路由,分成不同的组件。

dashboard-routing.module.ts仪表板-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 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>

The app-side-nav component consists of the router links. 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>

This seems to work fine because I can see the relevant content being loaded in the DOM depending on the button clicked.这似乎工作正常,因为我可以看到根据单击的按钮在 DOM 中加载的相关内容。 But for some reason, the content is not visible.但由于某种原因,内容不可见。 Here is a screenshot of the dashboard component loaded.这是加载的仪表板组件的屏幕截图。 For some reason, it is loading with a margin of 1920px but even when removed the content is still not visible.出于某种原因,它以 1920 像素的边距加载,但即使删除,内容仍然不可见。 应用程序 DOM

The structure seems to work yet there is something not quite right otherwise the content would show.该结构似乎有效,但有些地方不太正确,否则内容会显示。 Would appreciate any thoughts, suggestions or further questions.将不胜感激任何想法,建议或进一步的问题。

I think your proble is material think that your app-side-nav or mat-sidenav have a full screen width, on the screenshot as I can see, content have a 1920px margin-left, this means that your view out of the bound of screen.我认为您的问题很重要,认为您的 app-side-nav 或 mat-sidenav 具有全屏宽度,在我可以看到的屏幕截图中,内容的左侧边距为 1920px,这意味着您的视图超出了屏幕。 你的截图 There is my example with correct margin, pay attention to your nav component:我的示例具有正确的边距,请注意您的导航组件: 我的截图 Stackblitz example Hope this helps. Stackblitz 示例希望这会有所帮助。

After checking the rendered template it seems that your code lacks the routing precision.检查呈现的模板后,您的代码似乎缺乏路由精度。

Your app-routing.module.ts says that for path "" it should use DashboardModule .您的app-routing.module.ts说对于路径""它应该使用DashboardModule In the module you say that for path "" it should render a WrapperComponent .在模块中,您说对于路径""它应该呈现WrapperComponent And after this point it has no default route.在此之后,它没有默认路由。

So, basically what happens is that your wrapper component is rendered, however none of its children matches the route.所以,基本上发生的事情是你的包装器组件被渲染了,但是它的子组件都不匹配路由。 To fix it, you have two options:要修复它,您有两种选择:

  • provide handler for "" route in the wrappers children routes在包装子路由中为“”路由提供处理程序
  • add a redirect to one of the routes using a wildcard route as I did in the Stackblitz fork of your GitHub project.就像我在您的 GitHub 项目的 Stackblitz 分支中所做的那样,使用通配符路由向其中一个路由添加重定向。

Stackblitz fork Stackblitz 前叉

That is to fix that router outlet does not render anything.那就是修复路由器插座不渲染任何东西。

Another problem is <mat-sidenav #sideNav mode="side" opened="opened"> , as you are assigning a string to the boolean flag, which means that it will be always truthy and won't display what's underneath.另一个问题是<mat-sidenav #sideNav mode="side" opened="opened"> ,因为您正在为布尔标志分配一个字符串,这意味着它将始终是真实的并且不会显示下面的内容。

In result removing opened="opened" and adding a redirection to the dashboard page we can see the "dashboard works!"结果删除opened="opened"并添加重定向到仪表板页面,我们可以看到“仪表板工作!”

在此处输入图像描述

PS Looking at the examples in the docs it doesn't look to be designed to work the way you want it to. PS 查看文档中的示例,它看起来并没有按照您希望的方式工作。 Rather show or hide behavior.而是显示或隐藏行为。 Not expand.不展开。

PSS You could try using autoresize and toggle width manually for the ever open sidenav as suggested here PSS 您可以尝试按照此处的建议为永远打开的 sidenav 手动使用自动调整大小和切换宽度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM