angular 封裝公共方法

angular封裝公共方法到service中間件,節省開發時間html

layer.service.ts函數

openAlert(callback) {// 傳遞迴調函數
    const dialogRef = this.dialog.open(AlertComponent, { hasBackdrop: true, data: this.layer.alert }); // // alert關閉的時候會檢測到是哪一個關閉事件;
    dialogRef.afterClosed().subscribe(val => { if(callback){ callback(val); } }); }

組件接收調用this

demo.component.htmlspa

<button (click)="openConfirm()">open confirm</button>

demo.component.tscode

import {LayerService} from '../service/layer.service'; export class DemoComponent implements OnInit { constructor( private layerService:LayerService) { } // 打開confirm確認框
 openConfirm() { this.layerService.layer.confirm.content = '是否駁回?'
    this.layerService.openConfirm((val) => { if(val==0){ console.log('肯定--調接口'); console.log(val); } if(val==1){ console.log('取消--不調接口關閉彈層'); console.log(val); } if(val==2){ console.log('×--不調接口關閉彈層'); console.log(val); } }); } }
相關文章
相關標籤/搜索