SAP Spartacus CmsService的CmsActions.LoadCmsComponent

时间:2022-07-28
本文章向大家介绍SAP Spartacus CmsService的CmsActions.LoadCmsComponent,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在这个class的下列方法里加上console.log:

const loading$ = combineLatest([
      this.routingService.getNextPageContext(),
      this.store.pipe(
        select(CmsSelectors.componentsLoaderStateSelectorFactory(uid, context))
      ),
    ]).pipe(
      observeOn(queueScheduler),
      tap(([nextContext, loadingState]) => {
        const attemptedLoad =
          loadingState.loading || loadingState.success || loadingState.error;
        // if the requested context is the same as the one that's currently being navigated to
        // (as it might already been triggered and might be available shortly from page data)
        // TODO(issue:3649), TODO(issue:3668) - this optimization could be removed
        const couldBeLoadedWithPageData = nextContext
          ? serializePageContext(nextContext, true) === context
          : false;

        if (!attemptedLoad && !couldBeLoadedWithPageData) {
          console.log('baal try to load cp with uid: ' + uid
          + 'context: ' + JSON.stringify(pageContext));
          this.store.dispatch(
            new CmsActions.LoadCmsComponent({ uid, pageContext })
          );
        }
      })
    );

测试发现,在加载Spartacus首屏时,该方法(根据单个Component uid读取数据)不会被调用:

但是选中某个具体的产品进入明细页面后:

这个cmsActions来自: import { CmsActions } from ‘…/store/actions/index’;

到cms-group.actions里具体查看action实现:

由三个action文件组成。

根据关键字 LoadCmsComponent和LOAD_CMS_COMPONENT搜索:

上图loadComponent$负责加载。

调试结果: