SAP Spartacus CurrentProductService返回的null对象
使用如下的代码监控getProduct可能返回的null值:
import { Component, OnInit } from '@angular/core';
import { ActiveCartService, Product } from '@spartacus/core';
import { CurrentProductService } from '@spartacus/storefront';
import { Observable, OperatorFunction } from 'rxjs';
import { filter, map } from 'rxjs/operators';@Component({selector: 'app-mycom',templateUrl: './mycom.component.html',styleUrls: ['./mycom.component.scss']
})
export class MycomComponent implements OnInit {jerryfilter: OperatorFunction<Product, Product> =filter(this.jerrytest2);product$: Observable<Product> = this.currentProductService.getProduct().pipe(this.jerryfilter);productName$ = this.product$.pipe(map(product => product.name));jerrytest2(product: Product): boolean {const result = !!product;if (result === false) {console.log('false!');}return result;}constructor(private currentProductService: CurrentProductService, private cartService: ActiveCartService) {}ngOnInit(): void {this.product$.subscribe(product => console.log(product));}}
TypeScript语句jerryfilter: OperatorFunction<Product, Product> =
filter(this.jerrytest2);
对应的JavaScript函数:
jerryfilter的JavaScript实现是一个名字叫filter的function,接收一个predicate function作为输入参数,返回一个新的函数,总金额和新的函数会基于传进来的predicate新建一个filterOperator实例:
export function filter(predicate, thisArg) {return function filterOperatorFunction(source) {return source.lift(new FilterOperator(predicate, thisArg));};
product$: Observable = this.currentProductService.getProduct().pipe(this.jerryfilter);
注意,product$是一个Observable对象,不是实际的product值:
operator指向了我传入的filter.
product$也是Observable,因此可以继续调用pipe.
返回为false的情况:
filter的实现,如果箭头函数返回false,就不往下继续执行了:
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
SAP Spartacus CurrentProductService返回的null对象相关推荐
- SAP Spartacus CurrentProductService.getProduct方法的实现原理
注入CurrentProductService: 看看这个currentProductService又包含哪些属性: scope为undefined: 如果从routeState里能提取到produc ...
- SAP Spartacus cxFocus单元测试Expected null to equal -1的错误分析
当下图expect执行的时候,包裹在setTimeout里执行的this.requiredTabindex = -1赋值语句尚未得到执行的机会: 因此出错: persist-focus.directi ...
- Null对象模式的用法
1. 什么是Null对象模式?为什么要采用Null对象? 个人理解,所谓模式就是程序员们在编程的时候对一些类似问题总结出的通用解决方法或思路.因此,即使你的问题非常吻合某种设计模式所对应的问题,也不能 ...
- SAP Spartacus注入自定义的CurrentProductService
新建一个MyCurrentProductService,返回一个硬编码的产品主数据: 完整源代码如下: import { Product, ProductScope } from '@spartacu ...
- java返回空集合对象_返回null或空集合更好吗?
这是一个普遍的问题(但是我正在使用C#),最好的方法是什么(最佳实践),对于以集合为返回类型的方法,您是否返回null或空集合? #1楼 我想在这里举例说明. 在这里考虑一个案例. int total ...
- SAP Commerce Cloud WCMS 里的 home 页面和 SAP Spartacus Page API 返回的数据比较
You can only create new components in the WCMS Page View perspective. In the Live Edit perspective, ...
- SAP Spartacus 中的依赖注入 Dependency Injection 介绍
先了解 Angular 中的依赖注入 依赖项是指某个类执行其功能所需的服务或对象.依赖项注入(DI)是一种设计模式,在这种设计模式中,类会从外部源请求依赖项而不是让类自己来创建它们. Angular ...
- SAP Spartacus Definition of Done
SAP Spartacus Definition of Done Coding guidelines Spartacus 团队采用了以下一组规则来保持 Spartacus 代码的可读性和可维护性. 作 ...
- 如何查看 SAP Spartacus PageLayoutComponent 里的 template$ 的值
SAP Spartacus PageLayoutService.ts 里的 templateName$ get 操作:返回一个 Observable 对象,包裹的类型为 string. pipe 的第 ...
最新文章
- 【每日DP】day12、P1063 能量项链(区间DP又一模板,震惊,只需要4行代码?)难度⭐⭐⭐
- 中科院开源协会镜像站 Android SDK镜像测试发布
- SpringBoot开发案例之CountDownLatch多任务并行处理
- 电销机器人价格_箭鱼电销机器人:为什么电话机器人公司不用机器人给你打电话?...
- php 一片空白,解决运行PHP一片空白
- css3制作滚动按钮
- 客户端与服务器之间的文件传输,客户端与服务器的文件传输
- Docker学习总结(22)——Docke run命令详解
- lazarus控件怎么那么少_盘点5个常被搞混的控件与规范
- linux中手动配置ip命令,手动配置linux(centos)的IP地址
- javascript DOM对象转jquery对象
- MATLAB-画图汇总
- Hybrid APP介绍
- 408计算机考研2012真题解析,2018年计算机408统考考研真题及答案解析.pdf
- 计算机系徽 节徽设计,数学节节徽设计图
- 深圳禾正医院自控系统案例|能迪科技canlead中央空调净化系统
- 【UVA】P1510 Neon Sign
- 商业网站建设和运营的四度:Approachability、Usability、 Accessibility 和 Profitability...
- socket 读、写字节流数据
- 手机android内存不足怎么办,安卓手机内存不足怎么办 安卓手机内存不足怎么清理...