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

站長資訊網
最全最豐富的資訊網站

Angular學習之聊聊notification(自定義服務)

本篇文章帶大家繼續angular的學習,簡單了解一下angular中的自定義服務 notification,希望對大家有所幫助!

Angular學習之聊聊notification(自定義服務)

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

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

service 不僅可以用來處理 API 請求,還有其他的用處

比如,我們這篇文章要講到的 notification 的實現。【相關教程推薦:《angular教程》】

效果圖如下:

Angular學習之聊聊notification(自定義服務)

UI 這個可以后期調整

So,我們一步步來分解。

添加服務

我們在 app/services 中添加 notification.service.ts 服務文件(請使用命令行生成),添加相關的內容:

// notification.service.ts  import { Injectable } from '@angular/core'; import { Observable, Subject } from 'rxjs';  // 通知狀態的枚舉 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: ''   }    // 轉換成可觀察體   public getNotification(): Observable<NotificationStatus> {     return this.notify.asObservable();   }    // 進行中通知   public showProcessNotification() {     this.notify.next(NotificationStatus.Process)   }    // 成功通知   public showSuccessNotification() {     this.notify.next(NotificationStatus.Success)   }    // 結束通知   public showEndedNotification() {     this.notify.next(NotificationStatus.Ended)   }    // 更改信息   public changePrimarySecondary(primary?: string, secondary?: string) {     this.messageObj.primary = primary;     this.messageObj.secondary = secondary   }    constructor() { } }
登錄后復制

是不是很容易理解…

我們將 notify 變成可觀察物體,之后發布各種狀態的信息。

創建組件

我們在 app/components 這個存放公共組件的地方新建 notification 組件。所以你會得到下面的結構:

notification                                           ├── notification.component.html                     // 頁面骨架 ├── notification.component.scss                     // 頁面獨有樣式 ├── notification.component.spec.ts                  // 測試文件 └── notification.component.ts                       // javascript 文件
登錄后復制

我們定義 notification 的骨架:

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

接著,我們簡單修飾下骨架,添加下面的樣式:

// 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 的內容,在出現通知的時候才有   &.active {     top: 10px;   }   &.success {}   &.progress {}   &.failure {}   &.ended {} }
登錄后復制

success, progress, failure, ended 這四個類名對應 notification service 定義的枚舉,可以按照自己的喜好添加相關的樣式。

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

// notification.component.ts  import { Component, OnInit, HostBinding, OnDestroy } from '@angular/core'; // 新的知識點 rxjs import { Subscription } from 'rxjs'; import {debounceTime} from 'rxjs/operators'; // 引入相關的服務 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 {      // 防抖時間,只讀   private readonly NOTIFICATION_DEBOUNCE_TIME_MS = 200;      protected notificationSubscription!: Subscription;   private timer: any = null;   public message: string = ''      // notification service 枚舉信息的映射   private reflectObj: any = {     progress: "進行中",     success: "成功",     failure: "失敗",     ended: "結束"   }    @HostBinding('class') notificationCssClass = '';    public primaryMessage!: string;   public secondaryMessage!: string;    constructor(     private notificationService: NotificationService   ) { }    ngOnInit(): void {     this.init()   }    public init() {     // 添加相關的訂閱信息     this.notificationSubscription = this.notificationService.getNotification()       .pipe(         debounceTime(this.NOTIFICATION_DEBOUNCE_TIME_MS)       )       .subscribe((notificationStatus: NotificationStatus) => {         if(notificationStatus) {           this.resetTimeout();           // 添加相關的樣式           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 = ''   }      // 關閉定時器   private resetTimeout(): void {     if(this.timer) {       clearTimeout(this.timer)     }   }    // 關閉通知   public closeNotification() {     this.notificationCssClass = ''     this.resetTimeout()   }      // 組件銷毀   ngOnDestroy(): void {     this.resetTimeout();     // 取消所有的訂閱消息     this.notificationSubscription.unsubscribe()   }  }
登錄后復制

在這里,我們引入了 rxjs 這個知識點,RxJS 是使用 Observables 的響應式編程的庫,它使編寫異步或基于回調的代碼更容易。這是一個很棒的庫,接下來的很多文章你會接觸到它

贊(0)
分享到: 更多 (0)
?
網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
久久精品五月,日韩不卡视频在线观看,国产精品videossex久久发布 ,久久av综合
国产精品日韩久久久| 久久婷婷av| 亚洲精品日本| 日韩avvvv在线播放| 91精品久久久久久久久久不卡| 日韩三区免费| 电影亚洲精品噜噜在线观看 | 亚洲精品2区| 亚洲中午字幕| 日本h片久久| 成人一区不卡| 夜夜嗨一区二区三区| 夜久久久久久| 国产乱人伦精品一区| 国产成人免费| 亚洲在线网站| 国产精品久久久久久妇女| 欧美日韩国产观看视频| 国产精品日本| 久久xxx视频| 日韩不卡在线| 午夜视频一区二区在线观看| 国产精品第十页| 激情欧美亚洲| 91福利精品在线观看| av免费不卡国产观看| 美女被久久久| 亚洲国产成人精品女人| 日韩一区二区三区在线看| 国产成人久久精品一区二区三区| 亚洲激情二区| 精品国产成人| 亚洲日韩视频| 日韩av在线播放网址| 一区二区亚洲视频| 红杏一区二区三区| 久久高清一区| 国产成人精选| 日本va欧美va瓶| 99精品综合| 国产精品亚洲综合色区韩国| 午夜日韩福利| 免费在线播放第一区高清av| 天堂av在线一区| 中国字幕a在线看韩国电影| 蜜桃视频一区二区三区在线观看| 福利一区在线| 日韩欧美三区| 香蕉国产精品| 久久精品国产99国产| 一本一道久久a久久| 精品丝袜在线| 六月丁香综合在线视频| 亚洲深深色噜噜狠狠爱网站| 色在线视频观看| 国产精品欧美一区二区三区不卡 | 激情综合五月| 午夜性色一区二区三区免费视频| 久久精品欧美一区| 国产高清日韩| 日韩av一级片| 免费看欧美美女黄的网站| 久久伦理在线| 国产亚洲网站| 超碰在线99| 国产乱人伦丫前精品视频 | 麻豆精品久久久| 国产一区导航| 极品日韩av| 99精品美女| 鲁鲁在线中文| 激情中国色综合| 精品一区二区三区中文字幕视频| 日韩国产欧美三级| 亚洲欧洲美洲国产香蕉| 视频一区欧美日韩| 1024精品一区二区三区| 成人小电影网站| 成人午夜在线| 精品精品国产三级a∨在线| 国产乱码精品| 欧美日韩一区二区三区不卡视频 | 久久影视三级福利片| 亚洲青青久久| 亚洲91久久| 吉吉日韩欧美| 国产精品二区不卡| 黄在线观看免费网站ktv| 麻豆精品久久久| 国产精品成人国产| 免费在线成人| 国产成人精品一区二区三区免费 | 日韩美女国产精品| 日韩午夜视频在线| 日本欧美在线| 国产日韩欧美三级| 国产极品模特精品一二| 久久久91麻豆精品国产一区| 国产精品nxnn| 色爱综合网欧美| 国产精品不卡| 亚洲午夜在线| 中文一区一区三区免费在线观 | 午夜视频一区二区在线观看| 婷婷综合国产| 国产精品成人**免费视频| 欧美激情精品| 日本а中文在线天堂| 欧美日韩视频| 日韩avvvv在线播放| 精品国产一区二区三区av片| 久久夜夜操妹子| 丝袜亚洲另类欧美| 欧美日韩一区二区三区不卡视频| 久久精品国产久精国产| 日韩三区免费| 青青久久av| 国产美女精品| 国产精品欧美三级在线观看| 欧美好骚综合网| 伊人精品在线| 日韩高清成人在线| 久久精品国产一区二区| 午夜av成人| 欧美天堂亚洲电影院在线观看| 婷婷激情一区| 午夜一区在线| 国产精品综合色区在线观看| 精品一区视频| 欧美日韩国产传媒| 欧美精品国产| 黄色欧美在线| 婷婷综合社区| 日韩高清电影免费| 成人va天堂| 日韩不卡免费视频| 久久精品影视| 91福利精品在线观看| 日韩一区电影| 日韩一区网站| 久久视频国产| 国产精品香蕉| 在线亚洲成人| 国产成人在线中文字幕| 日韩中文字幕av电影| 久久久久免费| 日韩高清在线观看一区二区| 在线中文字幕播放| 亚洲69av| 欧美日韩激情| 麻豆国产精品777777在线| 国产亚洲一区在线| 国产一区日韩| 日产欧产美韩系列久久99| www.51av欧美视频| 亚洲精品国产精品粉嫩| 久久国产直播| 国产精品www994| 免费在线看一区| 久久精品观看| 国际精品欧美精品| 亚洲97av| 亚洲精品在线观看91| 九九99久久精品在免费线bt| 亚洲麻豆一区| 欧美日韩精品一本二本三本| 国产精品成人一区二区不卡| 7777精品| 亚洲婷婷丁香| 婷婷综合在线| 日韩一区欧美| 老鸭窝一区二区久久精品| 亚洲精品一级| 久久av在线| 欧美~级网站不卡| 中文在线资源| 久久亚洲道色| 欧美一区=区三区| 一本综合精品| 亚洲一区国产一区| 99视频精品全国免费| 欧美www视频在线观看| 国产精品a级| 国产美女精品视频免费播放软件| 在线精品亚洲| 久久av在线| 日韩午夜免费| 欧美69视频| 亚洲二区三区不卡| 亚洲小说欧美另类婷婷| 日韩中文影院| 在线天堂中文资源最新版| 国产精品毛片久久久| 国产精品欧美三级在线观看 | 国产亚洲久久| 91日韩在线| 国产精品手机在线播放| 亚洲综合中文| 男人的天堂亚洲一区| 久久夜色精品|