SAP Spartacus CurrentProductService返回的null对象
生活随笔
收集整理的這篇文章主要介紹了
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对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软证实“补丁周二”更新导致 Win11
- 下一篇: AWE2023|科大讯飞AI产品亮相:大