久久精品五月,日韩不卡视频在线观看,国产精品videossex久久发布 ,久久av综合

站長(zhǎng)資訊網(wǎng)
最全最豐富的資訊網(wǎng)站

Angular學(xué)習(xí)之聊聊notification(自定義服務(wù))

本篇文章帶大家繼續(xù)angular的學(xué)習(xí),簡(jiǎn)單了解一下angular中的自定義服務(wù) notification,希望對(duì)大家有所幫助!

Angular學(xué)習(xí)之聊聊notification(自定義服務(wù))

前端(vue)入門到精通課程,老師在線輔導(dǎo):聯(lián)系老師
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點(diǎn)擊使用

在之前的文章中,我們有提到:

service 不僅可以用來處理 API 請(qǐng)求,還有其他的用處

比如,我們這篇文章要講到的 notification 的實(shí)現(xiàn)。【相關(guān)教程推薦:《angular教程》】

效果圖如下:

Angular學(xué)習(xí)之聊聊notification(自定義服務(wù))

UI 這個(gè)可以后期調(diào)整

So,我們一步步來分解。

添加服務(wù)

我們?cè)?app/services 中添加 notification.service.ts 服務(wù)文件(請(qǐng)使用命令行生成),添加相關(guān)的內(nèi)容:

// notification.service.ts  import { Injectable } from '@angular/core'; import { Observable, Subject } from 'rxjs';  // 通知狀態(tài)的枚舉 export enum NotificationStatus {   Process = "progress",   Success = "success",   Failure = "failure",   Ended = "ended" }  @Injectable({   providedIn: 'root' }) export class NotificationService {    private notify: Subject<NotificationStatus> = new Subject();   public messageObj: any = {     primary: '',     secondary: ''   }    // 轉(zhuǎn)換成可觀察體   public getNotification(): Observable<NotificationStatus> {     return this.notify.asObservable();   }    // 進(jìn)行中通知   public showProcessNotification() {     this.notify.next(NotificationStatus.Process)   }    // 成功通知   public showSuccessNotification() {     this.notify.next(NotificationStatus.Success)   }    // 結(jié)束通知   public showEndedNotification() {     this.notify.next(NotificationStatus.Ended)   }    // 更改信息   public changePrimarySecondary(primary?: string, secondary?: string) {     this.messageObj.primary = primary;     this.messageObj.secondary = secondary   }    constructor() { } }
登錄后復(fù)制

是不是很容易理解…

我們將 notify 變成可觀察物體,之后發(fā)布各種狀態(tài)的信息。

創(chuàng)建組件

我們?cè)?app/components 這個(gè)存放公共組件的地方新建 notification 組件。所以你會(huì)得到下面的結(jié)構(gòu):

notification                                           ├── notification.component.html                     // 頁面骨架 ├── notification.component.scss                     // 頁面獨(dú)有樣式 ├── notification.component.spec.ts                  // 測(cè)試文件 └── notification.component.ts                       // javascript 文件
登錄后復(fù)制

我們定義 notification 的骨架:

<!-- notification.component.html -->  <!-- 支持手動(dòng)關(guān)閉通知 --> <button (click)="closeNotification()">關(guān)閉</button> <h1>提醒的內(nèi)容: {{ message }}</h1> <!-- 自定義重點(diǎn)通知信息 --> <p>{{ primaryMessage }}</p> <!-- 自定義次要通知信息 --> <p>{{ secondaryMessage }}</p>
登錄后復(fù)制

接著,我們簡(jiǎn)單修飾下骨架,添加下面的樣式:

// notification.component.scss  :host {   position: fixed;   top: -100%;   right: 20px;   background-color: #999;   border: 1px solid #333;   border-radius: 10px;   width: 400px;   height: 180px;   padding: 10px;   // 注意這里的 active 的內(nèi)容,在出現(xiàn)通知的時(shí)候才有   &.active {     top: 10px;   }   &.success {}   &.progress {}   &.failure {}   &.ended {} }
登錄后復(fù)制

success, progress, failure, ended 這四個(gè)類名對(duì)應(yīng) notification service 定義的枚舉,可以按照自己的喜好添加相關(guān)的樣式。

最后,我們添加行為 javascript 代碼。

// notification.component.ts  import { Component, OnInit, HostBinding, OnDestroy } from '@angular/core'; // 新的知識(shí)點(diǎn) rxjs import { Subscription } from 'rxjs'; import {debounceTime} from 'rxjs/operators'; // 引入相關(guān)的服務(wù) import { NotificationStatus, NotificationService } from 'src/app/services/notification.service';  @Component({   selector: 'app-notification',   templateUrl: './notification.component.html',   styleUrls: ['./notification.component.scss'] }) export class NotificationComponent implements OnInit, OnDestroy {      // 防抖時(shí)間,只讀   private readonly NOTIFICATION_DEBOUNCE_TIME_MS = 200;      protected notificationSubscription!: Subscription;   private timer: any = null;   public message: string = ''      // notification service 枚舉信息的映射   private reflectObj: any = {     progress: "進(jìn)行中",     success: "成功",     failure: "失敗",     ended: "結(jié)束"   }    @HostBinding('class') notificationCssClass = '';    public primaryMessage!: string;   public secondaryMessage!: string;    constructor(     private notificationService: NotificationService   ) { }    ngOnInit(): void {     this.init()   }    public init() {     // 添加相關(guān)的訂閱信息     this.notificationSubscription = this.notificationService.getNotification()       .pipe(         debounceTime(this.NOTIFICATION_DEBOUNCE_TIME_MS)       )       .subscribe((notificationStatus: NotificationStatus) => {         if(notificationStatus) {           this.resetTimeout();           // 添加相關(guān)的樣式           this.notificationCssClass = `active ${ notificationStatus }`           this.message = this.reflectObj[notificationStatus]           // 獲取自定義首要信息           this.primaryMessage = this.notificationService.messageObj.primary;           // 獲取自定義次要信息           this.secondaryMessage = this.notificationService.messageObj.secondary;           if(notificationStatus === NotificationStatus.Process) {             this.resetTimeout()             this.timer = setTimeout(() => {               this.resetView()             }, 1000)           } else {             this.resetTimeout();             this.timer = setTimeout(() => {               this.notificationCssClass = ''               this.resetView()             }, 2000)           }         }       })   }    private resetView(): void {     this.message = ''   }      // 關(guān)閉定時(shí)器   private resetTimeout(): void {     if(this.timer) {       clearTimeout(this.timer)     }   }    // 關(guān)閉通知   public closeNotification() {     this.notificationCssClass = ''     this.resetTimeout()   }      // 組件銷毀   ngOnDestroy(): void {     this.resetTimeout();     // 取消所有的訂閱消息     this.notificationSubscription.unsubscribe()   }  }
登錄后復(fù)制

在這里,我們引入了 rxjs 這個(gè)知識(shí)點(diǎn),RxJS 是使用 Observables 的響應(yīng)式編程的庫,它使編寫異步或基于回調(diào)的代碼更容易。這是一個(gè)很棒的庫,接下來的很多文章你會(huì)接觸到它

贊(0)
分享到: 更多 (0)
?
網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
久久精品五月,日韩不卡视频在线观看,国产精品videossex久久发布 ,久久av综合
999久久久91| 亚洲一二av| 国产手机视频一区二区 | 国产精品亚洲人成在99www| 日韩福利视频一区| 国产精品一区免费在线| 日韩国产高清在线| 欧美精品国产白浆久久久久| 电影91久久久| 成人精品亚洲| 蜜桃久久精品一区二区| 麻豆精品一区二区综合av| 三级精品视频| 1024精品久久久久久久久| 欧美中文高清| 国产精品1区| 在线午夜精品| 亚洲精品视频一二三区| 久久久久久婷| 欧美在线亚洲综合一区| 国产精品久久久亚洲一区| 麻豆精品久久久| 中国字幕a在线看韩国电影| 亚洲资源在线| 国产精品任我爽爆在线播放| 国产成人免费| 亚洲欧美日韩国产| 黄色在线观看www| 亚洲精品小说| 国产精品视频一区视频二区| 日韩欧美午夜| 欧美成a人片免费观看久久五月天| 国产91在线播放精品| 欧美~级网站不卡| 日本成人手机在线| 久久久久美女| 国产精品久久乐| 欧美日韩高清| 国产高清日韩| 丝袜亚洲另类欧美| 成人国产精品| 欧美日本三区| 亚洲自啪免费| 成人午夜毛片| 日韩高清中文字幕一区| 91九色精品| 久久免费精品| 日韩不卡手机在线v区| 精品国产鲁一鲁****| 玖玖精品视频| 亚洲爱爱视频| 久久精品亚洲| 婷婷视频一区二区三区| 日韩av免费| 久久精品资源| 四虎精品永久免费| 欧美日韩国产v| 最新国产精品| 久久视频精品| 中文字幕系列一区| 模特精品在线| 麻豆精品一区二区综合av| 国产亚洲在线| 综合日韩av| 午夜在线播放视频欧美| 日韩在线免费| 精品一区二区三区在线观看视频| 一区二区电影在线观看| 日韩在线观看不卡| 成人台湾亚洲精品一区二区| 欧美精品影院| 四虎在线精品| 麻豆亚洲精品| 美女亚洲一区| 久久男女视频| 久久精品青草| 久久精品中文| 午夜精品成人av| 国产精品精品国产一区二区| 国产精品日韩精品中文字幕| 91亚洲精品在看在线观看高清| 免费国产亚洲视频| 野花国产精品入口| 黄色成人在线网址| 欧美/亚洲一区| 伊人久久亚洲热| 激情综合网五月| 日韩一级欧洲| 久久av一区| 一区二区精品| 日韩精品亚洲专区在线观看| 日韩精品国产欧美| 国产欧美自拍| 美女国产精品久久久| 国产精品亚洲四区在线观看| 国产精品久久国产愉拍| 欧美国产极品| 久久免费视频66| 日韩成人精品一区| 免费观看不卡av| 一本色道精品久久一区二区三区| 亚洲一区日本| 亚洲免费观看高清完整版在线观| 亚洲乱亚洲高清| 国产日韩欧美一区在线| 美女久久精品| 日韩三区在线| 日韩精品一区第一页| 日韩中出av| 国产高清亚洲| 日韩一区二区三区免费播放| 激情综合在线| 中文字幕av一区二区三区人| 亚洲影视一区二区三区| 欧美日韩调教| 久久毛片亚洲| 亚洲中字黄色| 国产精品va| 99热国内精品| 久久国内精品自在自线400部| 免费看久久久| 久久天堂成人| 亚洲影院天堂中文av色| 欧美成人精品午夜一区二区| 欧美片第1页| 亚洲精品麻豆| 新版的欧美在线视频| 亚洲一区不卡| 日韩高清不卡在线| а√天堂8资源在线| 久久香蕉精品| 久久久精品国产**网站| 今天的高清视频免费播放成人| 四虎在线精品| 不卡一二三区| 亚洲三级av| av在线日韩| 日韩国产在线不卡视频| 麻豆网站免费在线观看| 亚洲一区久久| 国产成人精品一区二区三区在线| 欧美精品激情| 久久字幕精品一区| 一区在线免费观看| 国产精品久久久亚洲一区| 欧洲亚洲一区二区三区| 亚洲精品婷婷| 香蕉成人av| 国产毛片久久久| 午夜精品婷婷| 精品国产亚洲一区二区三区| 视频一区二区三区在线| аⅴ资源天堂资源库在线| 亚洲精品伊人| 久久青草久久| 欧美激情日韩| 亚洲精品美女| 国内精品99| 国产精品第一| 国产视频久久| 久久影院午夜精品| 国产免费av国片精品草莓男男| 精品一区免费| 91亚洲国产成人久久精品| 色8久久久久| 激情综合在线| 三上亚洲一区二区| 国产亚洲精品精品国产亚洲综合| 国产女优一区| 99久久九九| 国产日韩电影| 久久精品欧洲| 国产美女久久| 蜜桃久久av一区| 国内精品福利| 黄毛片在线观看| 久久99精品久久久久久园产越南| 亚洲精品观看| 国产精品女主播一区二区三区| 女生影院久久| 国产一区二区三区不卡视频网站 | 黄色av日韩| 国产盗摄——sm在线视频| 97久久亚洲| 日韩一区二区三区免费视频| 国产亚洲福利| 欧美日韩激情| 亚洲a一区二区三区| 国内精品伊人| 欧美国产先锋| 欧美一区二区三区久久精品| 亚洲午夜国产成人| 爽爽淫人综合网网站| 亚洲欧美日韩高清在线| 日韩精品欧美| 中文字幕在线视频网站| 国产一区一一区高清不卡| 国产精品亚洲欧美一级在线| 欧美一区久久| 久久国产三级精品|