久久精品五月,日韩不卡视频在线观看,国产精品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综合
天使萌一区二区三区免费观看| 蜜臀久久99精品久久久久宅男| 亚洲毛片在线免费| 国产一区二区三区自拍| 99久久精品网站| 蜜桃av一区二区在线观看| 亚洲欧美日韩国产| 日韩精品91| 伊人久久大香伊蕉在人线观看热v| 婷婷中文字幕一区| 欧美激情综合| 亚洲精品综合| 国产高潮在线| 日韩一区二区三区高清在线观看| 欧美日本不卡| 日本亚洲欧美天堂免费| 国产成人77亚洲精品www| 日韩精品一区第一页| 中文字幕在线视频网站| 久久国产精品色av免费看| 激情视频一区二区三区| 国语精品一区| 国产日韩一区二区三区在线播放| 午夜av一区| 成人午夜在线| 欧美国产亚洲精品| 欧美久久一区二区三区| 亚洲综合中文| 亚洲制服欧美另类| 久久先锋影音| 亚洲欧美日本日韩| 国产精品腿扒开做爽爽爽挤奶网站| 国产高清不卡| 亚洲1234区| 麻豆精品蜜桃| 亚洲一级黄色| 日韩毛片在线| 婷婷综合社区| 久久国产88| 久久丁香四色| 亚洲综合三区| 久久久久蜜桃| 色老板在线视频一区二区| 欧美激情aⅴ一区二区三区| 日韩一区二区三免费高清在线观看 | 日本一区二区三区视频在线看 | 久久久蜜桃一区二区人| 国产探花一区二区| 国产精品一区二区av日韩在线| 婷婷精品在线| 日韩国产91| 欧美激情麻豆| 精品视频99| 国产精品s色| 国产调教精品| 免费精品一区| 免费看久久久| jizzjizz中国精品麻豆| 欧美激情五月| 久久只有精品| 韩国一区二区三区视频| 精品高清久久| 欧美日韩亚洲在线观看| 国产精品7m凸凹视频分类| 国产亚洲福利| 日本中文字幕不卡| 麻豆精品新av中文字幕| 激情综合五月| 欧美国产小视频| 伊人影院久久| 日韩不卡一二三区| 捆绑调教日本一区二区三区| 一区三区视频| 丝袜美腿亚洲一区二区图片| 蜜桃传媒麻豆第一区在线观看| 青青草精品视频| 97精品一区二区| 亚洲制服一区| 日产精品一区二区| 亚洲一区二区三区高清不卡| 国产欧美三级| 日韩午夜av| 日本色综合中文字幕| 精品日韩视频| 国产欧美一区二区三区米奇| 久久九九国产| 国产日产一区| 水蜜桃久久夜色精品一区的特点| 欧美色综合网| 欧美日韩高清| 国内在线观看一区二区三区| 亚洲欧美日韩专区| 国产在线日韩精品| 日韩三级精品| 另类av一区二区| 啪啪国产精品| 精品一区二区三区中文字幕视频 | www在线观看黄色| 日本精品国产| 99pao成人国产永久免费视频| 伊伊综合在线| 欧美视频久久| 亚久久调教视频| 日韩手机在线| 亚洲欧美日韩国产一区二区| 人人香蕉久久| 亚洲免费播放| 美女毛片一区二区三区四区| 成人在线免费观看网站| 国产精品99精品一区二区三区∴| 日韩中文字幕视频网| 亚洲男女自偷自拍| 免费在线成人网| 日本欧美在线看| 亚洲一级淫片| **爰片久久毛片| 欧美精品成人| 国产欧美一区二区三区米奇| 国产精品天天看天天狠| 国产精一区二区| 国产极品模特精品一二| 狂野欧美性猛交xxxx| 国产成人精选| 精品一区毛片| 亚洲字幕久久| 国产日韩在线观看视频| 国内在线观看一区二区三区 | 欧美精品97| 日韩精品专区| 尤物tv在线精品| 国产农村妇女精品一区二区| 亚洲一区中文| 日韩黄色免费网站| 久久久久久色| 福利视频一区| 爽好多水快深点欧美视频| 国产色噜噜噜91在线精品| 成人欧美一区二区三区的电影| 婷婷综合网站| 久久99偷拍| 日韩中文字幕区一区有砖一区| 美女av一区| 激情综合网站| 欧美国产另类| 一区二区亚洲视频| 日韩免费福利视频| 免费在线看一区| 国产精品二区不卡| 蜜臀精品一区二区三区在线观看| 国产精品久久久久蜜臀| 日韩精品一区第一页| 成人综合一区| 日韩在线观看中文字幕| 亚洲国产专区校园欧美| 国产高清亚洲| 只有精品亚洲| 久久久久久久久久久妇女| 日韩高清不卡在线| 99热精品在线| 91偷拍一区二区三区精品| 日本不卡视频在线观看| 99国产精品一区二区| 国产精品sm| 国产区精品区| 日韩在线黄色| 综合激情网站| 久热精品在线| 今天的高清视频免费播放成人| 97成人在线| 亚洲视频国产精品| 在线观看亚洲精品福利片| 久久青青视频| 成人亚洲一区二区| 国产精品成人**免费视频 | 国产精品欧美一区二区三区不卡| 亚洲少妇一区| 久久青草久久| 精精国产xxxx视频在线野外| 国产欧美日韩精品高清二区综合区| 久久亚洲影院| 国产精品毛片在线| 欧美日韩亚洲在线观看| 夜鲁夜鲁夜鲁视频在线播放| 国产精品久久| 日本综合精品一区| 日韩中文欧美在线| 欧美在线影院| 欧美中文字幕一区二区| 麻豆视频在线观看免费网站黄| 欧美国产亚洲精品| 久久中文字幕一区二区三区| yellow在线观看网址| 蜜臀av免费一区二区三区| 狠狠爱www人成狠狠爱综合网| 首页欧美精品中文字幕| 91嫩草精品| 精品丝袜在线| 日本不卡高清视频| 国产aⅴ精品一区二区四区| 99精品视频在线观看免费播放|