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)文章
AngularJs1.x自定義指令獨(dú)立作用域的函數(shù)傳入?yún)?shù)方法
今天小編就為大家分享一篇AngularJs1.x自定義指令獨(dú)立作用域的函數(shù)傳入?yún)?shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10將angular-ui的分頁(yè)組件封裝成指令的方法詳解
這篇文章主要給大家介紹了將angular-ui的分頁(yè)組件封裝成指令的方法,文中介紹的非常詳細(xì),相信會(huì)對(duì)大家的學(xué)習(xí)或者工作覺有一定的參考價(jià)值,需要的朋友下面來一起看看吧。2017-05-05詳解Angular路由 ng-route和ui-router的區(qū)別
這篇文章主要介紹了詳解Angular路由 ng-route和ui-router的區(qū)別,分別介紹了兩種路由的用法和區(qū)別,有興趣的可以了解一下2017-05-05Angular2中constructor和ngOninit的使用講解
這篇文章主要介紹了Angular2中constructor和ngOninit的使用講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05Angular 數(shù)據(jù)請(qǐng)求的實(shí)現(xiàn)方法
本篇文章主要介紹了Angular 數(shù)據(jù)請(qǐng)求的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05AngularJS之自定義服務(wù)詳解(factory、service、provider)
本篇文章主要介紹了AngularJS之自定義服務(wù)詳解(factory、service、provider),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04