Angular 6 - 材料



材料为您的项目提供了许多内置模块。自动完成、日期选择器、滑块、菜单、网格和工具栏等功能可用于 Angular 6 中的材料。

要使用材料,我们需要导入包。Angular 2 也拥有所有上述功能,但它们作为 @angular/core 模块的一部分提供。Angular 6 推出了一个单独的模块@angular/materials.。这有助于用户导入所需的材料。

要开始使用材料,您需要安装两个包 - 材料和 cdk。材料组件依赖于动画模块以实现高级功能,因此您需要为此使用动画包,即 @angular/animations。该包已在上一章中更新。

npm install --save @angular/material @angular/cdk

现在让我们看看 package.json。@angular/material@angular/cdk 已安装。

{
  "name": "angular6-app",
  "version": "0.0.0",
  "scripts": {
      "ng": "ng",
      "start": "ng serve",
      "build": "ng build",
      "test": "ng test",
      "lint": "ng lint",
      "e2e": "ng e2e"
   },
   "private": true, "dependencies": {
      "@angular/animations": "^6.1.0",
      "@angular/cdk": "^6.4.7",
      "@angular/common": "^6.1.0",
      "@angular/compiler": "^6.1.0",
      "@angular/core": "^6.1.0",
      "@angular/forms": "^6.1.0",
      "@angular/http": "^6.1.0",
      "@angular/material": "^6.4.7",
      "@angular/platform-browser": "^6.1.0",
      "@angular/platform-browser-dynamic": "^6.1.0",
      "@angular/router": "^6.1.0",
      "core-js": "^2.5.4",
      "rxjs": "^6.0.0",
      "zone.js": "~0.8.26"
   },
   "devDependencies": {
      "@angular-devkit/build-angular": "~0.7.0",
      "@angular/cli": "~6.1.3",
      "@angular/compiler-cli": "^6.1.0",
      "@angular/language-service": "^6.1.0",
      "@types/jasmine": "~2.8.6",
      "@types/jasminewd2": "~2.0.3",
      "@types/node": "~8.9.4",
      "codelyzer": "~4.2.1",
      "jasmine-core": "~2.99.1",
      "jasmine-spec-reporter": "~4.2.1",
      "karma": "~1.7.1",
      "karma-chrome-launcher": "~2.2.0",
      "karma-coverage-istanbul-reporter": "~2.0.0",
      "karma-jasmine": "~1.1.1",
      "karma-jasmine-html-reporter": "^0.2.2",
      "protractor": "~5.3.0",
      "ts-node": "~5.0.1",
      "tslint": "~5.9.1",
      "typescript": "~2.7.2"
   }
}

我们突出显示了安装以使用材料的包。

我们现在将在父模块app.module.ts中导入模块,如下所示。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatButtonModule, MatMenuModule, MatSidenavModule } from '@angular/material';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
      BrowserModule,
      BrowserAnimationsModule,
      MatButtonModule,
      MatMenuModule,
      FormsModule,
      MatSidenavModule
   ],
   providers: [],
   bootstrap: [AppComponent]
})
export class AppModule { }

在上面的文件中,我们从 @angular/materials 导入了以下模块。

import { MatButtonModule, MatMenuModule, MatSidenavModule } from '@angular/material';

并在下面的 imports 数组中使用它:

imports: [
   BrowserModule,
   BrowserAnimationsModule,
   MatButtonModule,
   MatMenuModule,
   FormsModule,
   MatSidenavModule
]

app.component.ts 如下所示:

import { Component } from '@angular/core';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   myData: Array<any>;
   constructor() {}
}

现在让我们在styles.css中添加材料 CSS 支持。

@import "~@angular/material/prebuilt-themes/indigo-pink.css";

现在让我们在app.component.html中添加材料。

<button mat-button [matMenuTriggerFor] = "menu">Menu</button>
<mat-menu #menu = "matMenu">
   <button mat-menu-item>
      File
   </button>
   <button mat-menu-item>
      Save As
   </button>
</mat-menu>
<mat-sidenav-container class = "example-container">
   <mat-sidenav #sidenav class = "example-sidenav">
      Angular 6
   </mat-sidenav>
   <div class = "example-sidenav-content">
      <button type = "button" mat-button  (click) = "sidenav.open()">
         Open sidenav
      </button>
   </div>
</mat-sidenav-container>

在上面的文件中,我们添加了菜单和侧边栏。

菜单

要添加菜单,使用<mat-menu></mat-menu>文件另存为项添加到mat-menu下的按钮下。添加了一个主按钮菜单。它的引用通过使用[matMenuTriggerFor]="menu"传递给<mat-menu>,并使用#在<mat-menu>中使用菜单。

侧边栏

要添加侧边栏,我们需要<mat-sidenav-container></mat-sidenav-container><mat-sidenav></mat-sidenav>作为容器的子元素添加。添加了另一个 div,它通过使用(click)="sidenav.open()"触发侧边栏。以下是菜单和侧边栏在浏览器中的显示:

Open Sidenav Menu

点击opensidenav后,它会显示如下所示的侧边栏:

Open Sidenav Side Bar

点击菜单后,您将获得两个项目文件另存为,如下所示:

Click Open Sidenav Shows Item

现在让我们使用材料添加日期选择器。要添加日期选择器,我们需要导入显示日期选择器所需的模块。

app.module.ts中,我们导入了以下模块,如下所示,用于日期选择器。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatDatepickerModule, MatInputModule, MatNativeDateModule } from '@angular/material';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
      BrowserModule,
      BrowserAnimationsModule,
      FormsModule,
      MatDatepickerModule,
      MatInputModule,
      MatNativeDateModule
   ],
   providers: [],
   bootstrap: [AppComponent]
})
export class AppModule { }

这里,我们导入了MatDatepickerModule、MatInputModuleMatNativeDateModule等模块。

现在,app.component.ts如下所示:

import { Component } from '@angular/core';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   myData: Array<any>;
   constructor() {}
}

app.component.html如下所示:

<mat-form-field>
   <input matInput [matDatepicker] = "picker" placeholder = "Choose a date">
   <mat-datepicker-toggle matSuffix [for] = "picker"></mat-datepicker-toggle>
   <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

这就是日期选择器在浏览器中显示的方式。

Datepicker Is Displayed
广告

© . All rights reserved.