久久精品五月,日韩不卡视频在线观看,国产精品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| 欧美精品二区| 国产成人77亚洲精品www| 丝袜诱惑制服诱惑色一区在线观看| 蜜桃视频在线观看一区| 日韩一区二区三区精品视频第3页| 九九九精品视频| 免费在线成人网| 日韩精选在线| 成人一区而且| 亚洲aⅴ网站| 亚洲性视频h| 国产精品自拍区| 久久国产高清| 久久三级视频| 久久99国产精品视频| 欧美视频二区| 天堂√中文最新版在线| 久久精品毛片| 日韩精品电影一区亚洲| 国产一区导航| 亚洲一级特黄| 久久婷婷激情| 日韩中文一区二区| 国产精品主播| 麻豆精品蜜桃视频网站| 国产美女久久| 国产专区精品| 亚洲精品字幕| 天堂网在线观看国产精品| 精品国产亚洲日本| 日韩视频一区| 精品国产一区二区三区2021| 婷婷综合社区| 久久精品色播| 日本不卡一区二区| 亚洲女同一区| 国产精品嫩草99av在线| 国产伦精品一区二区三区千人斩| 国产综合精品| 国产激情久久| 亚洲欧美网站在线观看| 亚洲91视频| 亚洲成人免费| 久草免费在线视频| 欧美天堂在线| 亚洲欧美网站在线观看| 成人在线免费观看网站| 日韩av中文字幕一区二区三区| 亚洲一区免费| 国产日韩在线观看视频| 亚洲精品第一| 欧美国产另类| 亚洲精品一二| 99国内精品| 久久精品国语| 国产aⅴ精品一区二区四区| 91精品尤物| 日韩一区二区免费看| 日韩中文在线播放| re久久精品视频| 免费在线日韩av| 日韩精品一区二区三区中文 | 中文在线а√天堂| 国产精品久久久久久久久久妞妞| 六月丁香综合| 国产一区清纯| 香蕉视频亚洲一级| 国产成人在线中文字幕| 国产精品porn| 国产亚洲一区二区三区啪| 视频一区二区中文字幕| 亚洲午夜精品久久久久久app| 日韩.com| 精品国产三区在线| 国产精品一区二区av日韩在线| 久久av在线| 91久久中文| 一级欧洲+日本+国产| 亚洲成人不卡| 日韩av在线播放网址| 精品欧美日韩精品| 国内在线观看一区二区三区| 国产精品99精品一区二区三区∴| 91精品国产自产观看在线 | 久久精品国产亚洲一区二区三区| 国产精品一区高清| 国产精品一区三区在线观看| 国产精品一区二区三区美女| 国产毛片精品| 国产精品久久久久77777丨| 欧美日本三区| 欧美激情三区| 国产成人精品三级高清久久91| 水蜜桃久久夜色精品一区| 首页国产精品| 久久精品亚洲人成影院| 欧美亚洲精品在线| 午夜国产一区二区| 国产精品毛片| 国产一级成人av| 国产高清不卡| 国产精品7m凸凹视频分类| 国际精品欧美精品| 波多视频一区| 国产99久久久国产精品成人免费| 91精品一区二区三区综合| 999国产精品视频| 欧美~级网站不卡| 狠狠爱成人网| 宅男噜噜噜66国产日韩在线观看| 男女精品网站| 日本欧美在线| 免费观看亚洲天堂| 麻豆成全视频免费观看在线看| 国产一区一一区高清不卡| 97精品国产| 激情偷拍久久| 一区二区电影在线观看| 日韩av黄色在线| 国模精品一区| 欧美在线亚洲综合一区| 亚洲色图国产| 国产精品nxnn| 久久九九精品| 亚洲精品一二三**| 麻豆免费精品视频| 999久久久91| 亚洲三级网址| 精品三级在线观看视频| 久久久人人人| 亚洲免费成人av在线| 精品国产一区二区三区2021| 韩日一区二区三区| 日本欧美韩国一区三区| 精品国产a一区二区三区v免费| 99久久夜色精品国产亚洲狼| 中文不卡在线| 国产一区福利| 黄色日韩在线| 久久国产生活片100| 樱桃视频成人在线观看| 综合国产精品| 波多野结衣久久精品| 在线观看视频免费一区二区三区| 91成人精品观看| 色88888久久久久久影院| 免费成人在线视频观看| 久久91视频| 日av在线不卡| 国产一区三区在线播放| 天堂成人国产精品一区| 麻豆一区二区三| 久久亚洲影院| 超级白嫩亚洲国产第一| 亚洲香蕉网站| 久久伦理在线| 亚洲综合图色| 精品视频一区二区三区在线观看| 久久久久.com| 只有精品亚洲| 欧美aⅴ一区二区三区视频| 精精国产xxxx视频在线野外| 性欧美长视频| 国产精品久久久久久av公交车| 精品亚洲美女网站| 视频一区视频二区中文字幕| 久久久久久夜| 欧美日韩三区| 日韩中文字幕在线一区| 精品国产不卡| 日韩午夜免费| 国产精品亲子伦av一区二区三区 | 国产精品第一国产精品| 欧美aa一级| 亚洲精品一二三**| 伊伊综合在线| 日韩高清电影一区| 四虎8848精品成人免费网站| 亚洲综合另类| 精品三区视频| 亚洲香蕉视频| 天堂日韩电影| 欧美日本二区| 91p九色成人| 欧美日韩国产欧| 精品欧美日韩精品| 日韩高清三区| 免费日韩视频| 91久久久精品国产| 久久精品国产久精国产| 裤袜国产欧美精品一区| 婷婷综合成人| 久久一区二区三区电影| 国产精品久久久久久久久免费高清| 日韩午夜免费| 欧美日韩a区|