• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >建站教程 > Angular利用service实现自定义服务(notification)

Angular利用service实现自定义服务(notification)

作者:站长图库 字体:[增加 减小] 来源:互联网

站长图库向大家介绍了Angular,service自定义服务,notification等相关知识,希望对您有所帮助

本篇文章带大家继续angular的学习,了解一下Angular怎么利用service实现自定义服务(notification),希望对大家有所帮助!


Angular利用service实现自定义服务(notification)


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

service 不仅可以用来处理 API 请求,还有其他的用处

比如,我们这篇文章要讲到的 notification 的实现。

效果图如下:


Angular利用service实现自定义服务(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';// 新的知识点 rxjsimport { 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()  }   p
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 什么是依赖注入?在Angular中怎么实现?
  • Angular CLI发布路径的配置项浅析
  • 浅析Angular中HttpClientModule模块有什么用?怎么用?
  • 浅谈Angular中elem.scope()、elem.isolateScope和$compile(elem)(scope)中scope的区别
  • 详解Angular中的Observable(可观察对象)
  • 浅析Angular+rxjs怎么实现拖拽功能?
  • 聊聊在Angular项目中怎么实现权限控制?
  • Angular中怎么自定义视频播放器
  • 详解Angular中的NgModule(模块)
  • Angular学习之以Tooltip为例了解自定义指令

相关文章

  • Photoshop制作金属质感的工具图标
  • 关于出现“对不起,您安装的不是正版应用..”的解决办法
  • Photoshop绘制蓝色立体效果的软件图标
  • 最新消息!WordPress 5.8开始将不再支持IE11浏览器
  • PS打造中国风水墨草书毛笔书法字体设计制作教程
  • Thinkphp+layer+ajax如何完成增加方法(附代码示例)
  • 一文讲解ajax实现无刷新上传和下载(代码详解)
  • 论坛网站推广案例分析
  • 做好网站站内链接分配 提高网站百度指日可待
  • LayUI如何实现数据分页功能

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • Photoshop设计立体喜庆的舞台效果图
    • 如何修改mysql的默认时区
    • Illustrator CS5绘制逼真的红辣椒教程
    • Photoshop制作彩色效果艺术字教程
    • 教你使用PHP数据库迁移工具“Phinx”
    • PhotoShop设计制作梦幻炫彩光斑文字效果教程
    • 数据库独立性是指数据库和什么相互独立?
    • WordPress url链接去掉category的方法插件即可搞定
    • PS制作漂亮的‘少了你世间万种都成单调’古风图
    • PhotoShop制作魔幻霓虹火焰字效果的教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有