使用Angular CDK實(shí)現(xiàn)一個(gè)Service彈出Toast組件功能
在Angular中,官方團(tuán)隊(duì)在開(kāi)發(fā)Material組件庫(kù)的同時(shí),順手做了一套Component dev kit,也就是在Angular世界中大名鼎鼎的CDK,這套工具包提供了非常多的前端開(kāi)發(fā)的通用功能。Angular的知名組件庫(kù)幾乎都依賴(lài)了這套開(kāi)發(fā)包。比如ANT,PrimeNG等。
本文主要寫(xiě)用cdk實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Toast組件,使用的是cdk中的overlay模塊。
1.環(huán)境安裝
cdk不是angular的默認(rèn)模塊,需要手動(dòng)安裝 yarn add @angular/cdk
在app.module中引入cdk中的OverlayModule
import { OverlayModule } from '@angular/cdk/overlay'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, OverlayModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
2.創(chuàng)建Toast組件和ToastService
- 使用
ng g c Toast
命令快速創(chuàng)建一個(gè)組件模版 - 使用
ng g s Toast
創(chuàng)建一個(gè)Service的模版
2.1編寫(xiě)Toast組件和樣式
ToastComponent
<div class="q-toast"> <div class="q-toast-mask"></div> <p class="q-toast-msg">{{msg}}</p> </div> .q-toast { padding: .2rem .5rem; width: 5rem; position: relative; display: flex; flex-direction: row; align-items: center; justify-content: center; .q-toast-mask { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #000; opacity: .8; border-radius: 2rem; } .q-toast-msg { color: white; z-index: 999; } }
ToastService
import { Overlay, OverlayConfig } from '@angular/cdk/overlay'; import { ComponentPortal } from '@angular/cdk/portal'; import { Injectable, InjectionToken, Injector } from '@angular/core'; import { ToastComponent } from './toast.component'; @Injectable({ providedIn: 'root' }) export class ToastService { constructor(private overlay: Overlay) { } Show(msg: string) { const config = new OverlayConfig(); const positionStrategy = this.overlay.position() .global().centerVertically().centerHorizontally(); config.positionStrategy = positionStrategy; let overlayRef = this.overlay.create(config); const inject = Injector.create({ providers: [ { provide: Toast_Ref, useValue: overlayRef }, { provide: Toast_Msg, useValue: msg } ] }) console.log(inject.get<string>(Toast_Ref)) let partal = new ComponentPortal(ToastComponent, null, inject); overlayRef.attach(partal) setTimeout(() => { overlayRef.detach() overlayRef.dispose(); }, 2000); } } export const Toast_Ref = new InjectionToken<{}>('Toast_Ref'); export const Toast_Msg = new InjectionToken<{}>('Toast_Msg');
使用Toast
編寫(xiě)好Service后,只需要Angular會(huì)默認(rèn)注入到root模塊,只需要在需要彈出Toast的組件的構(gòu)造方法寫(xiě)上對(duì)應(yīng)的ToastService就可以正常運(yùn)行了。
export class AppComponent { constructor(private toast:ToastService) { } test() { this.toast.Show('hello cdk!') } }
gif效果圖
到此這篇關(guān)于使用Angular CDK實(shí)現(xiàn)一個(gè)Service彈出Toast組件的文章就介紹到這了,更多相關(guān)Angular CDK 實(shí)現(xiàn)Toast組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
div實(shí)現(xiàn)自適應(yīng)高度的textarea實(shí)現(xiàn)angular雙向綁定
本文主要介紹了div實(shí)現(xiàn)自適應(yīng)高度的textarea,實(shí)現(xiàn)angular雙向綁定的方法。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01AngularJS 中的指令實(shí)踐開(kāi)發(fā)指南(一)
指令(Directives)是所有AngularJS應(yīng)用最重要的部分。盡管AngularJS已經(jīng)提供了非常豐富的指令,但還是經(jīng)常需要?jiǎng)?chuàng)建應(yīng)用特定的指令。這篇教程會(huì)為你講述如何自定義指令,以及介紹如何在實(shí)際項(xiàng)目中使用2016-03-03AngularJs實(shí)現(xiàn)ng1.3+表單驗(yàn)證
這篇文章主要介紹了AngularJs實(shí)現(xiàn)ng1.3+表單驗(yàn)證,感興趣的小伙伴們可以參考一下2015-12-12AngularJS實(shí)現(xiàn)按鈕提示與點(diǎn)擊變色效果
這篇文章給大家介紹了如何利用AngularJS實(shí)現(xiàn)按鈕提示與點(diǎn)擊變色的效果,文中提供了實(shí)例代碼,對(duì)大家學(xué)習(xí)AngularJS具有一定的參考借鑒價(jià)值,下面來(lái)一起看看吧。2016-09-09詳解Angular Reactive Form 表單驗(yàn)證
本文我們將介紹 Reactive Form 表單驗(yàn)證的相關(guān)知識(shí),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07AngularJS 與Bootstrap實(shí)現(xiàn)表格分頁(yè)實(shí)例代碼
這篇文章主要介紹了AngularJS 與Bootstrap實(shí)現(xiàn)表格分頁(yè)的相關(guān)資料,并附實(shí)例代碼和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-10-10Angular+ionic實(shí)現(xiàn)折疊展開(kāi)效果的示例代碼
這篇文章主要介紹了Angular+ionic實(shí)現(xiàn)折疊展開(kāi)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07