詳解angular2封裝material2對話框組件
1. 說明
angular-material2自身文檔不詳,控件不齊,使用上造成了很大的障礙。這里提供一個方案用于封裝我們最常用的alert和confirm組件。
2. 官方使用方法之a(chǎn)lert
①編寫alert內(nèi)容組件
@Component({
template : `<p>你好</p>`
})
export class AlertComponent {
constructor(){
}
}
②在所屬模塊上聲明
//必須聲明兩處 declarations: [ AlertComponent], entryComponents : [ AlertComponent]
③使用MdDialg.open方法打開
//注入MdDialog對象
constructor(private mdDialog : MdDialog) { }
//打開
this.mdDialog.open(AlertComponent)
3. 官方使用方法之confirm
①編寫confirm內(nèi)容組件
@Component({
template : `<div md-dialog-title>'確認操作'</div>
<div md-dialog-content>確認執(zhí)行操作?</div>
<div md-dialog-actions>
<button md-button (click)="mdDialogRef.close('ok')">確認</button>
<button md-button (click)="mdDialogRef.close('cancel')">取消</button>
</div>`
})
export class ConfirmComponent {
constructor(private mdDialogRef : MdDialogRef<DialogComponent>){ }
}
②在所屬模塊上聲明
//必須聲明兩處 declarations: [ ConfirmComponent], entryComponents : [ ConfirmComponent]
③使用MdDialog.open打開并訂閱相關(guān)事件
//注入MdDialog對象
constructor(private mdDialog : MdDialog) { }
//打開
this.mdDialog.open(ConfirmComponent).subscribe(res => {
res === 'ok' && dosomething
});
4. 分析
如2、3所示,使用material2的對話框組件相當繁瑣,甚至僅僅打開一個不同的alert都要聲明一個獨立的組件,可用性很差。但也不是毫無辦法。
MdDialog.open原型:
open<T>(componentOrTemplateRef: ComponentType<T> | TemplateRef<T>, config?: MdDialogConfig): MdDialogRef<T>;
其中MdDialogConfig:
export declare class MdDialogConfig {
viewContainerRef?: ViewContainerRef;
/** The ARIA role of the dialog element. */
role?: DialogRole;
/** Whether the user can use escape or clicking outside to close a modal. */
disableClose?: boolean;
/** Width of the dialog. */
width?: string;
/** Height of the dialog. */
height?: string;
/** Position overrides. */
position?: DialogPosition;
/** Data being injected into the child component. */
data?: any;
}
具體每一個配置項有哪些用途可以參考官方文檔,這里data字段,說明了將會被攜帶注入子組件,也即被open打開的component組件。怎么獲取呢?
config : any;
constructor(private mdDialogRef : MdDialogRef<AlertComponent>){
this.config = mdDialogRef.config.data || {};
}
有了它我們就可以定義一個模板型的通用dialog組件了。
5. 定義通用化的組件
//alert.component.html
<div class="title" md-dialog-title>{{config?.title || '提示'}}</div>
<div class="content" md-dialog-content>{{config?.content || ''}}</div>
<div class="actions" *ngIf="!(config?.hiddenButton)" md-dialog-actions>
<button md-button (click)="mdDialogRef.close()">{{config?.button || '確認'}}</button>
</div>
//alert.component.scss
.title, .content{
text-align: center;
}
.actions{
display: flex;
justify-content: center;
}
//alert.component.ts
@Component({
selector: 'app-alert',
templateUrl: './alert.component.html',
styleUrls: ['./alert.component.scss']
})
export class AlertComponent {
config : {};
constructor(private mdDialogRef : MdDialogRef<AlertComponent>){
this.config = mdDialogRef.config.data || {};
}
}
我們將模板的一些可置換內(nèi)容與config一些字段進行關(guān)聯(lián),那么我們可以這么使用:
constructor(private mdDialog : MdDialog) { }
let config = new MdDialogConfig();
config.data = {
content : '你好'
}
this.mdDialog.open(AlertComponent, config)
依然繁瑣,但至少我們解決了對話框組件復(fù)用的問題。
我們可以聲明一個新的模塊,暫且起名為CustomeDialogModule,然后將component聲明在此模塊里,再將此模塊聲明到AppModule,這樣可以避免AppModule的污染,保證我們的對話框組件獨立可復(fù)用。
6. 二次封裝
如果僅僅是上面的封裝,可用性依然很差,工具應(yīng)當盡可能的方便,所以我們有必要再次進行封裝
首先在CustomDialogModule建一個服務(wù),暫且起名為CustomDialogService
@Injectable()
export class CustomDialogService {
constructor(private mdDialog : MdDialog) { }
//封裝confirm,直接返回訂閱對象
confirm(contentOrConfig : any, title ?: string) : Observable<any>{
let config = new MdDialogConfig();
if(contentOrConfig instanceof Object){
config.data = contentOrConfig;
}else if((typeof contentOrConfig) === 'string'){
config.data = {
content : contentOrConfig,
title : title
}
}
return this.mdDialog.open(DialogComponent, config).afterClosed();
}
//同
alert(contentOrConfig : any, title ?: string) : Observable<any>{
let config = new MdDialogConfig();
if(contentOrConfig instanceof Object){
config.data = contentOrConfig;
}else if((typeof contentOrConfig) === 'string'){
config.data = {
content : contentOrConfig,
title : title
}
}
return this.mdDialog.open(AlertComponent, config).afterClosed();
}
我們把它注冊在CustomDialogModule里的provides,它就可以被全局使用了。
用法:
constructor(dialog : CustomDialogService){}
this.dialog.alert('你好');
this.dialog.alert('你好','標題');
this.dialog.alert({
content : '你好',
title : '標題',
button : 'ok'
});
this.dialog.confirm('確認嗎').subscribe(res => {
res === 'ok' && dosomething
});
按照這種思路我們還可以封裝更多組件,例如模態(tài)框,toast等
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
- 詳解Angular 4.x 動態(tài)創(chuàng)建組件
- Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解
- Angular2學(xué)習教程之組件中的DOM操作詳解
- angular中不同的組件間傳值與通信的方法
- Angular父組件調(diào)用子組件的方法
- Angular5給組件本身的標簽添加樣式class的方法
- Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解(下)
- Angular入口組件(entry component)與聲明式組件的區(qū)別詳解
- angular2倒計時組件使用詳解
- 簡單談?wù)凙ngular中的獨立組件的使用
相關(guān)文章
Angular 2 ngForm中的ngModel、[ngModel]和[(ngModel)]的寫法
本篇文章主要介紹了Angular 2 ngForm中的ngModel、[ngModel]和[(ngModel)]的區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
angular6的table組件開發(fā)的實現(xiàn)示例
這篇文章主要介紹了angular6的table組件開發(fā)的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
詳解Angularjs 如何自定義Img的ng-load 事件
本篇文章主要介紹了詳解Angularjs 如何自定義Img的ng-load 事件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
angular2/ionic2 實現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例
這篇文章主要介紹了angular2/ionic2 實現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
AngularJS 應(yīng)用身份認證的技巧總結(jié)
這篇文章主要介紹了AngularJS 應(yīng)用身份認證的技巧總結(jié),具有一定的參考價值,有需要的可以了解一下。2016-11-11
Angular 4 依賴注入學(xué)習教程之FactoryProvider的使用(四)
這篇文章主要給大家介紹了關(guān)于Angular 4 依賴注入之FactoryProvider使用的相關(guān)資料,文中介紹的非常詳細,對大家學(xué)習或者使用Angular4具有一定的參考學(xué)習價值,需要的朋友們下面來一起看看吧。2017-06-06

