Material(包括Material Icon)在Angular2中的使用詳解
1.引入material npm包
npm install @angular/material @angular/cdk
2.新建一個(gè)ebiz-material.module.ts方便管理引入material的module
ng g module ebiz-material -app=ebiz-ui
3.在app的根module中引入ebiz-material.module.ts
import { EbizMaterialModule } from './ebiz-material/ebiz-material.module';
@NgModule({
imports: [..., EbizMaterialModule],
declarations: [
...
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
使用material組件
1.首先在ebiz-material.module.ts中引入material組件的module,例如我們要用到checkbox
(https://material.angular.io/components/checkbox/overview),那就引入MatCheckboxModule,引入之后再exports。
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatCheckboxModule } from '@angular/material';
@NgModule({
imports: [CommonModule, MatCheckboxModule],
declarations: [],
exports: [ MatCheckboxModule ]
})
export class EbizMaterialModule { }
2.在html文件中使用組件
<mat-checkbox [(ngModel)]="checked">Check me!</mat-checkbox>
使用material-icon
1.引入material-icon
npm install material-design-icons
如果下載失?。ㄎ沂莍nstall失敗了,也不去管它了,能用就行),可以到 github上 下載下來,然后取出iconfont文件夾放到自己的項(xiàng)目目錄下,并且在需要用到圖標(biāo)的css(scss)中引入,一般情況我們會(huì)放在style.scss中全局去使用。
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url(assets/iconfont/MaterialIcons-Regular.eot); /* For IE6-8 */
src: local('Material Icons'),
local('MaterialIcons-Regular'),
url(assets/iconfont/MaterialIcons-Regular.woff2) format('woff2'),
url(assets/iconfont/MaterialIcons-Regular.woff) format('woff'),
url(assets/iconfont/MaterialIcons-Regular.ttf) format('truetype');
}
/* meterial icon的設(shè)定 */
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px; /* Preferred icon size */
display: inline-block;
line-height: 1;
text-transform: none;
letter-spacing: normal;
word-wrap: normal;
white-space: nowrap;
direction: ltr;
/* Support for all WebKit browsers. */
-webkit-font-smoothing: antialiased;
/* Support for Safari and Chrome. */
text-rendering: optimizeLegibility;
/* Support for Firefox. */
-moz-osx-font-smoothing: grayscale;
/* Support for IE. */
font-feature-settings: 'liga';
}
2.在html的適當(dāng)位置放上圖標(biāo)
<i class="material-icon">iconName<i>
使用material內(nèi)置theme以及自定義theme
1.material中的組件會(huì)根據(jù)theme的不同,會(huì)有不一樣的樣式呈現(xiàn),但是這些樣式的不同只局限于material組件內(nèi)部,不會(huì)影響自定義組件的樣式。
2.styles.css文件名改為styles.scss,并且在angular-cli.json文件中修改為
"styles": [
"styles.scss"
],
3.在style.scss文件中引入material預(yù)建主題(總共4個(gè))
@import '~@angular/material/prebuilt-themes/deeppurple-amber.css'; @import '~@angular/material/prebuilt-themes/indigo-pink.css'; @import '~@angular/material/prebuilt-themes/pink-bluegrey.css'; @import '~@angular/material/prebuilt-themes/purple-green.css';
4.如果覺得這些主題不適合,可以自定義主題,在styles.scss同級(jí)目錄下新建一個(gè)theme.scss,并寫上自定義主題的內(nèi)容(https://material.angular.io/guide/theming)
@import '~@angular/material/theming'; @include mat-core(); $my-app-primary: mat-palette($mat-blue); $my-app-accent: mat-palette($mat-teal, A200, A100, A400); $my-app-warn: mat-palette($mat-red); $my-app-theme: mat-light-theme($my-app-primary, $my-app-accent, $my-app-warn); @include angular-material-theme($my-app-theme);
5.在步驟3中用到了一些顏色,例如$mat-blue,可以參考這里
6.如果想要對(duì)某個(gè)組件進(jìn)行主題特制,可以參考這里
7.在styles.scss中引入自定義主題
@import './theme';
總結(jié)
以上所述是小編給大家介紹的Material(包括Material Icon)在Angular2中的使用,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
AngularJS頁面帶參跳轉(zhuǎn)及參數(shù)解析操作示例
這篇文章主要介紹了AngularJS頁面帶參跳轉(zhuǎn)及參數(shù)解析操作,結(jié)合具體實(shí)例形式分析了AngularJS使用URL傳遞參數(shù)及參數(shù)的接收、解析等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
AngularJS中使用three.js的實(shí)例詳解
這篇文章主要介紹了AngularJS中使用three.js的實(shí)例詳解,我將之前自己做的demo放到了angularJS的一個(gè)component中,其實(shí)一開始是沒有準(zhǔn)備用框架的但是后面發(fā)現(xiàn)需要進(jìn)行的雙向綁定越來越多,后期表單數(shù)據(jù)的變化量也很大,最后還是選擇用NG來做這些事情2017-07-07
AngularJS教程之MVC體系結(jié)構(gòu)詳解
本文主要講解AngularJS MVC體系結(jié)構(gòu),這里提供詳細(xì)的教程供大家學(xué)習(xí)參考,有需要的小伙伴可以參考下2016-08-08
關(guān)于Angular2 + node接口調(diào)試的解決方案
這篇文章主要給大家介紹了關(guān)于Angular2 + node接口調(diào)試的解決方案,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05
angularjs+bootstrap實(shí)現(xiàn)自定義分頁的實(shí)例代碼
本篇文章主要介紹了angularjs+bootstrap實(shí)現(xiàn)自定義分頁的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

