如何在 angular 8 和 angular material 中单击 sidenav 中的菜单时在侧边栏旁边显示我的组件
How to display my component next to side bar on click of menu from sidenav in angular 8 and angular material
我正在研究 Angular 8 和 Angular material 但我被困在我想在 sidenav 旁边显示我的内容或(组件)的地方,即当任何 [=侧面菜单中的 70=] 是 clicked.Here 是我的一段代码
Link 的 stackblitz My_link
除了 App 组件,我还有 3 个组件
- Header 组件
- 边栏组件
- Link内容组件
SidebarCompoment.html
<div style="height: 100vh;">
<mat-sidenav-container>
<mat-sidenav #sidenav>
<!-- # [(opened)]="opened" mode="side" (opened)="log('Opened')"
(closed)="log('Closed')" -->
<div class="dropdown">
<button [matMenuTriggerFor]="animals" class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Animal index</button>
</div>
<mat-menu #animals="matMenu">
<button mat-menu-item [matMenuTriggerFor]="vertebrates" (click)="setflag()"><a [routerLink]="'/link1'">Vertebrates</a></button>
<button mat-menu-item [matMenuTriggerFor]="invertebrates">Invertebrates</button>
</mat-menu>
</mat-sidenav>
<mat-sidenav-content>
<button class="fa fa-list fa-2x" (click)="sidenav.toggle()"></button>
</mat-sidenav-content>
</mat-sidenav-container>
</div>
SidebarComponent.css
mat-sidenav-container {
height: 100vh;
background-color: white;
}
mat-sidenav, mat-sidenav-content {
padding: 16px;
}
mat-sidenav {
background-color: black;
width: 300px;
}
app-routing.module.ts
const routes: Routes = [
{ path: '', component: AppComponent},
{ path: 'link1', component: LinkContentComponent},
{ path: 'link2', component: LinkContentComponent}
];
app.component.html
<app-header></app-header>
<app-sidebar></app-sidebar>
<router-outlet></router-outlet>
我还向 stackblitz 提供 link
Stackblitz-link
所以这里我的问题是当我从侧面菜单中单击任何 link 时,我想在主屏幕中加载 Link 内容组件,紧挨着侧面菜单,就在 [=69= 下方] 。那么当我单击 link 时,我现在应该怎么做 that.Right 组件加载到侧边栏组件下方,如 figure.Any 帮助所示
查看您的代码,我认为您需要重新访问文档。
https://material.angular.io/components/sidenav/overview
当我去找你 Link 的 stackblitz 时,我可以看到你在 app.component.html.So 中有你的路由器插座你可以做的是像这样在 sidebar.component.html 中添加你的路由器插座
Sidebar.component.html
<div style="height: 100vh;">
<mat-sidenav-container>
<mat-sidenav #sidenav>
<!-- # [(opened)]="opened" mode="side" (opened)="log('Opened')"
(closed)="log('Closed')" -->
<div class="dropdown">
<button [matMenuTriggerFor]="animals" class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Animal index</button>
</div>
<mat-menu #animals="matMenu">
<button mat-menu-item [matMenuTriggerFor]="vertebrates" (click)="setflag()"><a [routerLink]="'/link1'">Vertebrates</a></button>
<button mat-menu-item [matMenuTriggerFor]="invertebrates">Invertebrates</button>
</mat-menu>
</mat-sidenav>
<mat-sidenav-content>
<!-- Adding router-outtlet here -->
<router-outlet></router-outlet>
<button class="fa fa-list fa-2x" (click)="sidenav.toggle()"></button>
</mat-sidenav-content>
</mat-sidenav-container>
</div>
我正在研究 Angular 8 和 Angular material 但我被困在我想在 sidenav 旁边显示我的内容或(组件)的地方,即当任何 [=侧面菜单中的 70=] 是 clicked.Here 是我的一段代码
Link 的 stackblitz My_link
除了 App 组件,我还有 3 个组件
- Header 组件
- 边栏组件
- Link内容组件
SidebarCompoment.html
<div style="height: 100vh;">
<mat-sidenav-container>
<mat-sidenav #sidenav>
<!-- # [(opened)]="opened" mode="side" (opened)="log('Opened')"
(closed)="log('Closed')" -->
<div class="dropdown">
<button [matMenuTriggerFor]="animals" class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Animal index</button>
</div>
<mat-menu #animals="matMenu">
<button mat-menu-item [matMenuTriggerFor]="vertebrates" (click)="setflag()"><a [routerLink]="'/link1'">Vertebrates</a></button>
<button mat-menu-item [matMenuTriggerFor]="invertebrates">Invertebrates</button>
</mat-menu>
</mat-sidenav>
<mat-sidenav-content>
<button class="fa fa-list fa-2x" (click)="sidenav.toggle()"></button>
</mat-sidenav-content>
</mat-sidenav-container>
</div>
SidebarComponent.css
mat-sidenav-container {
height: 100vh;
background-color: white;
}
mat-sidenav, mat-sidenav-content {
padding: 16px;
}
mat-sidenav {
background-color: black;
width: 300px;
}
app-routing.module.ts
const routes: Routes = [
{ path: '', component: AppComponent},
{ path: 'link1', component: LinkContentComponent},
{ path: 'link2', component: LinkContentComponent}
];
app.component.html
<app-header></app-header>
<app-sidebar></app-sidebar>
<router-outlet></router-outlet>
我还向 stackblitz 提供 link Stackblitz-link
所以这里我的问题是当我从侧面菜单中单击任何 link 时,我想在主屏幕中加载 Link 内容组件,紧挨着侧面菜单,就在 [=69= 下方] 。那么当我单击 link 时,我现在应该怎么做 that.Right 组件加载到侧边栏组件下方,如 figure.Any 帮助所示
查看您的代码,我认为您需要重新访问文档。 https://material.angular.io/components/sidenav/overview
当我去找你 Link 的 stackblitz 时,我可以看到你在 app.component.html.So 中有你的路由器插座你可以做的是像这样在 sidebar.component.html 中添加你的路由器插座
Sidebar.component.html
<div style="height: 100vh;">
<mat-sidenav-container>
<mat-sidenav #sidenav>
<!-- # [(opened)]="opened" mode="side" (opened)="log('Opened')"
(closed)="log('Closed')" -->
<div class="dropdown">
<button [matMenuTriggerFor]="animals" class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Animal index</button>
</div>
<mat-menu #animals="matMenu">
<button mat-menu-item [matMenuTriggerFor]="vertebrates" (click)="setflag()"><a [routerLink]="'/link1'">Vertebrates</a></button>
<button mat-menu-item [matMenuTriggerFor]="invertebrates">Invertebrates</button>
</mat-menu>
</mat-sidenav>
<mat-sidenav-content>
<!-- Adding router-outtlet here -->
<router-outlet></router-outlet>
<button class="fa fa-list fa-2x" (click)="sidenav.toggle()"></button>
</mat-sidenav-content>
</mat-sidenav-container>
</div>