我使用Angular Material创建了一个工具栏。但是,它没有响应。如何使工具栏响应?
工具栏代码:
<md-toolbar color = "primary">
<button md-button class="md-primary" [routerLink]="['/basic']"><md-icon class = "icon-20">home</md-icon> Angular Concepts</button>
<button md-button [mdMenuTriggerFor]="bMenu">Basic Concepts</button>
<md-menu #bMenu="mdMenu">
<button md-menu-item [routerLink]="['/a4']">Angular Component</button>
<button md-menu-item [routerLink]="['/cli ']">Angular CLI</button>
<button md-menu-item [routerLink]="['/inout']">Event Emitters</button>
<button md-menu-item [routerLink]="['/template']">Template Driven Forms</button>
<button md-menu-item [routerLink]="['/reactive']">Data Driven Forms</button>
<button md-menu-item [routerLink]="['/directives']">Angular Custom Directives</button>
<button md-menu-item [routerLink]="['/pipes']">Custom Pipes</button>
<button md-menu-item [routerLink]="['/viewchild']">View Child</button>
<button md-menu-item [routerLink]="['/view']">View Encapsulation</button>
</md-menu>
<button md-button [mdMenuTriggerFor]="aMenu">Advanced Concepts</button>
<md-menu #aMenu="mdMenu">
<button md-menu-item [routerLink]="['/ngrx']">Angular Redux using ngrx/store</button>
<button md-menu-item [routerLink]="['/guard']">Angular Guards</button>
<button md-menu-item [routerLink]="['/host']">Host & Host-Context</button>
</md-menu>
<button md-button (click)="openDialog()"> Contact Card</button>
</md-toolbar>
答案 0 :(得分:14)
这是我最喜欢的在Angular中创建响应式导航栏的方法。如果使用Angular 6,请确保使用6.1+版本。
关于Stackblitz的工作示例:https://stackblitz.com/edit/angular-v6xwte
以下是具体操作步骤:
1)安装必要的软件包。输入您的终端:
npm install --save @angular/material @angular/cdk @angular/animations
npm install @angular/flex-layout --save
2)在app.module.ts
中导入必要的模块import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FlexLayoutModule } from '@angular/flex-layout';
import {
MatIconModule, MatButtonModule, MatSidenavModule, MatToolbarModule
} from '@angular/material';
请记住将这些模块添加到下面的imports数组中。
3)将“材质图标”链接添加到您的index.html
链接必须位于任何Angular内容之前。
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
4)在您的styles.css中添加一个Angular主题并将边距设置为0%
@import "~@angular/material/prebuilt-themes/indigo-pink.css";
body{
margin: 0%;
}
5)在您的app.component.html中添加工具栏HTML代码
<div>
<mat-toolbar color="primary">
<div fxShow="true" fxHide.gt-sm="true">
<button mat-icon-button (click)="sidenav.toggle()">
<mat-icon>menu</mat-icon>
</button>
</div>
<a mat-button class="companyName" routerLink="/">
<span>Site name</span>
</a>
<span class="example-spacer"></span>
<div fxShow="true" fxHide.lt-md="true">
<a mat-button routerLink="/about-us">About us</a>
<a mat-button routerLink="/prices">Prices</a>
<a mat-button routerLink="/start-page">Start page</a>
<a mat-button routerLink="/offer">Offer</a>
<a mat-button routerLink="/contact">Contact</a>
</div>
</mat-toolbar>
<mat-sidenav-container fxFlexFill class="example-container">
<mat-sidenav color="primary" #sidenav fxLayout="column" mode="over" opened="false" fxHide.gt-sm="true">
<div fxLayout="column">
<a mat-button routerLink="/about-us">About us</a>
<a mat-button routerLink="/prices">Prices</a>
<a mat-button routerLink="/start-page">Start page</a>
<a mat-button routerLink="/offer">Offer</a>
<a mat-button routerLink="/contact">Contact</a>
</div>
</mat-sidenav>
<mat-sidenav-content fxFlexFill>
Awesome content
</mat-sidenav-content>
</mat-sidenav-container>
</div>
6)在app.component.css中设置工具栏样式
.companyName{
font-size: 150%;
}
.mat-toolbar{
height: 7vh;
}
div {
overflow: inherit;
}
.mat-sidenav-container{
background-color: lightskyblue;
min-height: 93vh !important;
}
a{
text-decoration: none;
font-size: 110%;
white-space: normal;
}
button{
font-size: 110%;
min-width: min-content;
}
.example-icon {
padding: 0 14px;
}
.example-spacer {
flex: 1 1 auto;
}
.mat-sidenav-content{
font-size: 200%;
text-align: center;
}
答案 1 :(得分:4)
答案 2 :(得分:3)
<强>问题强>
您需要一种方法来使导航栏响应。
<强>解决方案强>
您可以将Material Angular与ToolBar和Flex-Layout一起使用。
示例强>
npm install @angular/flex-layout --save
具有Flex布局的角度材质工具栏示例。这是一个很棒的教程here。
<div style="height: 100vh;">
<mat-toolbar color="primary">
<span>Responsive Navigation</span>
<span class="example-spacer"></span>
<div fxShow="true" fxHide.lt-md="true">
<!-- The following menu items will be hidden on both SM and XS screen sizes -->
<a href="#" mat-button>Menu Item 1</a>
<a href="#" mat-button>Menu Item 2</a>
<a href="#" mat-button>Menu Item 3</a>
<a href="#" mat-button>Menu Item 4</a>
<a href="#" mat-button>Menu Item 5</a>
<a href="#" mat-button>Menu Item 6</a>
</div>
<div fxShow="true" fxHide.gt-sm="true">
<a href="#" (click)="sidenav.toggle()">Show Side Menu</a>
</div>
</mat-toolbar>
<mat-sidenav-container fxFlexFill class="example-container">
<mat-sidenav #sidenav fxLayout="column">
<div fxLayout="column">
<a (click)="sidenav.toggle()" href="#" mat-button>Close</a>
<a href="#" mat-button>Menu Item 1</a>
<a href="#" mat-button>Menu Item 2</a>
<a href="#" mat-button>Menu Item 3</a>
<a href="#" mat-button>Menu Item 4</a>
<a href="#" mat-button>Menu Item 5</a>
<a href="#" mat-button>Menu Item 6</a>
</div>
</mat-sidenav>
<mat-sidenav-content fxFlexFill>Main content</mat-sidenav-content>
</mat-sidenav-container>
</div>
答案 3 :(得分:0)
我使用flex-layout和angular-material 5.0.2进行粗略的实现尝试
.mat-sidenav-container {
background: rgba(0, 0, 0, 0.08);
}
.blank-grow {
flex: 1 1 auto;
}
<mat-sidenav-container fullscreen>
<mat-sidenav #sidenav>
<mat-nav-list>
<a mat-list-item>
<mat-icon mat-list-icon>home</mat-icon>
<span mat-line>home</span>
</a>
<a mat-list-item>
<mat-icon mat-list-icon>backup</mat-icon>
<span mat-line>Backup</span>
</a>
</mat-nav-list>
</mat-sidenav>
<mat-toolbar color="primary">
<button mat-icon-button (click)="sidenav.open()" fxHide="false" fxHide.gt-sm>
<mat-icon>menu</mat-icon>
</button>
<span> Big Header</span>
<span class="blank-grow"></span>
<div fxLayout="row" fxShow="false" fxShow.gt-sm>
<a>
<mat-icon mat-list-icon>home</mat-icon>
<span mat-line>home</span>
</a>
<a>
<mat-icon mat-list-icon>backup</mat-icon>
<span mat-line>Backup</span>
</a>
</div>
</mat-toolbar>
</mat-sidenav-container>