日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何获取 SAP Commerce Cloud Spartacus UI 购物车 Cart 的加载状态

發布時間:2023/12/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何获取 SAP Commerce Cloud Spartacus UI 购物车 Cart 的加载状态 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在 Storefront AppModule 構造函數里注入 ActiveCartService:

private cartService: ActiveCartService,


調用其 API:

const loading$ = this.cartService.getLoading();loading$.subscribe((data) => console.log('Jerry cart loading? ', data));

打印出的日志:

active-cart.service.d.ts 里,僅僅包含方法的參數定義:

如果要查看其實現代碼,還是得去 fesm2015 的 Spartacus-core.js 文件里查看:

getLoading(): Observable<boolean> {return this.cartSelector$.pipe(map((cartEntity) => cartEntity.loading),distinctUntilChanged());}

查看 this.cartSelector$ 的實現:

// Stream with active cart entityprotected cartSelector$ = this.activeCartId$.pipe(switchMap((cartId) => this.multiCartService.getCartEntity(cartId)));

cartSelector$ 的賦值邏輯:從 activeCartId$ 里取出 cartId,調用 multiCartService 讀取。

MultiCartService 也只是從 store 里通過selector 去讀取。

ActiveCartId$ 的賦值邏輯:

// This stream is used for referencing carts in API calls.protected activeCartId$ = this.userIdService.getUserId().pipe(// We want to wait with initialization of cartId until we have userId initialized// We have take(1) to not trigger this stream, when userId changes.take(1),switchMapTo(this.store),select(MultiCartSelectors.getActiveCartId),// We also wait until we initialize cart from localStorage. Before that happens cartId in store === nullfilter((cartId) => cartId !== activeCartInitialState),map((cartId) => {if (cartId === '') {// We fallback to current when we don't have particular cart id -> cartId === '', because that's how you reference latest user cart.return OCC_CART_ID_CURRENT;}return cartId;}));

在這個文件處加上一行打印語句:

果然看到這條語句了:

為什么初始的 loading 標志位為 true?

通過調試代碼得知:

這個 LoadCart 繼承了 EntityLoadAction,所以也繼承了默認的 load:true 標志位。

那么什么時候又變成 false 呢?

調用棧看完了都沒看到有 Spartacus 相關的代碼:

只知道肯定發生在 Load Cart Success 之后。

從 map.js 里看到了線索:這個 value 里包含了購物車 cart 的業務數據:

以及 loading:false

這是我們的應用代碼:

從 Cart selector 里取出 CartEntity,調用 map,將 loading 字段映射出來:

Cart 數據結構:

從這里也能說明,一定是 Load Cart Success 觸發的第二次 loading 打印:

此時 cart 數據已經回來了,loading 為 false:

更多Jerry的原創文章,盡在:“汪子熙”:

總結

以上是生活随笔為你收集整理的如何获取 SAP Commerce Cloud Spartacus UI 购物车 Cart 的加载状态的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。