官方链接:https://sap.github.io/spartacus-docs/event-service/#page-title

The Spartacus event service provides a stream of events that you can consume without a tight integration to specific components or modules. The event system is used in Spartacus to build integrations to third party systems, such as tag managers and web trackers.

Spartacus 事件服务提供了一个事件流,您可以使用这些事件流,而无需与特定组件或模块紧密集成。 Spartacus 中使用事件系统来构建与第三方系统的集成,例如标签管理器和网络跟踪器。

The event service also allows you to decouple certain components. For example, you might have a component that dispatches an event, and another component that reacts to this event, without requiring any hard dependency between the components.

事件服务还允许您解耦某些组件。 例如,您可能有一个分派事件的组件和另一个对该事件做出反应的组件,而无需组件之间的任何硬依赖。

一个例子:

import { CxEvent } from "@spartacus/core";
export class CartAddEntryEvent extends CxEvent {cartId: string;userId: string;productCode: string;quantity: number;
}

在 app module 里监听这个事件的代码:

export class AppModule {constructor(events: EventService, myAdapter: OccCartAdapter) {const result$ = events.get(CartAddEntrySuccessEvent);result$.subscribe((event) => console.log(event));}
}

运行时,我一旦将某个产品加到购物车里,就会触发上面 app module 里注册的匿名函数的 console.log, 打印出 CartAddEntrySuccessEvent 实例的值。

Pulling Additional Data From Facades - 从 Facade 中提取额外数据

如果您需要比特定事件中包含的数据更多的数据,您可以将此数据与其他流组合。 例如,您可以从 facade 收集额外的数据。

以下是对“添加到购物车事件”做出反应的示例,然后等待购物车 stable(因为需要从后端重新加载 OCC 购物车),然后将所有购物车数据附加到事件数据:

constructor(events: EventService,cartService: ActiveCartService){}
/* ... */const result$ = this.events.get(CartAddEntrySuccessEvent).pipe(// When the above event is captured, wait for the cart to be stable// (because OCC reloads the cart after any cart operation)...switchMap((event) =>this.cartService.isStable().pipe(filter(Boolean), mapTo(event))),// Merge the state snapshot of the cart with the data from the event:withLatestFrom(this.cartService.getActive()),map(([event, cart]) => ({ ...event, cart }))
);

运行时效果:

更多Jerry的原创文章,尽在:“汪子熙”:

SAP Spartacus 事件服务 Event Service 使用介绍相关推荐

  1. 关于 SAP Spartacus 注入服务 UserAccountService 和 facade 的问题

    原始问题 issue 里提到的 UserAccountService: 实现了 UserAccountFacade. 如果直接导入 UserAccountService: import { UserA ...

  2. SAP Spartacus breakpoint服务中platformID的注入逻辑

    @Inject(PLATFORM_ID) protected platform: any 这个PLATFORM_ID来自@angular/core: 在platform-browser.js里能查看到 ...

  3. SAP Spartacus Tag Management System TMS 介绍

    注意:此功能是在 Spartacus 库的 3.2 版中引入的. Spartacus 标签管理系统 (TMS) 允许您设置标签管理器,并指定应将哪些 Spartacus 事件传递给配置的 TMS. S ...

  4. Jerry在2020 SAP全球技术大会的分享:SAP Spartacus技术介绍的文字版

    这是Jerry 2020年的第86篇文章,也是汪子熙公众号总共第268篇原创文章. 这篇文章的视频版本如下: https://v.qq.com/x/page/b3212l6kqvg.html 这个分享 ...

  5. SAP Spartacus 中的依赖注入 Dependency Injection 介绍

    先了解 Angular 中的依赖注入 依赖项是指某个类执行其功能所需的服务或对象.依赖项注入(DI)是一种设计模式,在这种设计模式中,类会从外部源请求依赖项而不是让类自己来创建它们. Angular ...

  6. SAP Spartacus B2B ListComponent响应回车事件的实现

    SAP Spartacus B2B的List页面: 和回车键事件相关的实现: <cx-table*ngIf="data.values?.length > 0; else empt ...

  7. SAP S/4HANA Service Management和SAP FSM基于CPI的集成场景介绍

    本文作者是我的同事,Song Hao(宋浩),SAP成都研究院S/4HANA Service Management的开发人员. 项目背景 相信大家已经知道,2018年6月份,SAP收购了一家专注于Fi ...

  8. SAP S4CRM 1811 服务订单API介绍

    Jerry在今年2月28日,SAP Customer Management for S/4HANA 1.0正式问世这个具有纪念意义的日子,同时发布了中英文版的博客进行介绍. 英文版发在SAP社区上,至 ...

  9. Linux网络服务-Web Service之【apache的功能、安装、配置文件介绍以及实验实例】(三)...

    上两张博文: < Linux网络服务-Web Service之[HTTP协议简介](一) > <Linux网络服务-Web Service之[Apache-Prefork.Worke ...

最新文章

  1. 给卡车穿上“隐身衣”,让自动驾驶车辆撞上它!这场自动驾驶比赛,比谁攻得快...
  2. 更新源列表及设置方法
  3. SAP Fiori Elements - Account F4 help - how value help dialog is populated
  4. NHibernate.3.0.Cookbook第三章第9节的翻译
  5. go和python性能对比_Go和Python Web服务器性能对比
  6. 相关不是因果,哪又是啥?
  7. 内网网络摄像机(RTSP/IPC/NVR)如何能在公网进行RTMP/HLS/HTTP-FLV直播
  8. A*算法收敛最优点的简单证明
  9. 计算机找不到WPS云盘,为什么我的电脑里没有WPS网盘入口?开启教程教给你!
  10. SSH框架java写接口_SSH框架的搭建+JPA接口
  11. 论文阅读:VoltJockey: Breaching TrustZone by Software-Controlled Voltage Manipulation over
  12. zeplin 登录效果实现
  13. 分布电容和杂散电容_寄生电容 分布电容
  14. 大连理工计算机应用基础作业,2014秋大连理工大学《计算机应用基础》在线测试1...
  15. jndi weblogic mysql_在WebLogic新建针对Oracle数据库的JNDI数据源
  16. 微信小程序如何转云开发
  17. 仿网易云音乐html代码,仿网易云音乐外链播放器UI的HTML5音乐播放器插件
  18. Mysql数据库报错:Row size too large ( 8126). Changing some columns to TEXT or BLOB or using ROW_FORMAT=DY
  19. Android Architecture(Is Activity God?)
  20. concurrentHashMap代码走读 chm走读

热门文章

  1. 【Android开发坑系列】之事件
  2. CSS/Compass修改placeholder的文字样式
  3. 《D3.js数据可视化实战手册》—— 1.1 简介
  4. Maven 中的pom.xml文件
  5. Android隐藏状态栏、导航栏
  6. iOS 字典与字符串之间的互转
  7. BZOJ2459 : [BeiJing2011]神秘好人
  8. 真机运行报错:Could not change executable permissions on the application
  9. 开学前要多为孩子健康做准备
  10. 什么时候考虑使用神经网络