spartacus-storefront - SAP Spartacus - 如何在 Spartacus UI 中显示自定义组件
问题描述
我有一个自定义组件可以在我的 UI 中将图像显示为平铺。
对于从后端创建的自定义组件,我收到如下响应。
组件:“Test1Component Test2Component Test3Component Test4Component Test5Component Test6Component Test7Component Test8Component” 容器:“false” 修改时间:“2020-09-29T20:52:40.454+05:30” 名称:“Tiles Component” typeCode:“TileCollectionComponent” uid:“TilesCollectionComponent” uuid:“eyJpdGVtSWQiOiJUaWxlc0NvbGxlY3Rpb25Db21wb25lbnQiLCJjYXRhbG9nSWQiOiJhbHNheWVyLXNwYUNvbnRlbnRDYXRhbG9nIiwiY2F0YWxvZ1ZlcnNpb24iOiJPbmxpbmUifQ=
如何检索数据并将其显示在 UI 中。
解决方案
组件数据表明该组件是所谓的容器组件。容器组件只响应组件 UID。这有点不幸,因为您需要与 Spartacus 中的 cms 服务进行交互以解析组件类型。在您开始渲染它们之前。
我们的代码库中确实有一个示例,您可以使用它来呈现这些嵌套组件。您在https://github.com/SAP/spartacus/blob/532603fbcfcf7c21c3abbf4e342fcda03652b61e/projects/storefrontlib/src/cms-components/content/banner-carousel/banner-carousel.component.ts#L25中看到我们被选中了组件数据和switchMap
this 到一个CmsComponentData
. (使用cmsService.getComponentData
)。Spartacus 将循环中的不同并行请求自动合并到组件数据的单个后端。
您可以CmsComponentData
在 ngFor 循环中使用该数组,并利用cxComponentWrapper
迭代所有CmsComponentData
对象。将cxComponentWrapper
映射到正确的组件并添加其他内容,例如 smartedit 集成。
<ng-container [cxComponentWrapper]="component"></ng-container>
尽管这一切都可用,但您的问题让我想到构建一个更简单的解决方案来完成繁重的工作。
推荐阅读
- python - 在 Bokeh JS 回调中将 .filter() 应用于 ColumnDataSource
- azure-devops - AzureDevops 分支权限设置
- python - 尝试按两个读取单列文本文件以在字典中创建键值对
- python - 错误:无法提取课程 ID;- 乌迪米
- html - flexbox 中的项目会自动调整它们的宽度,以便它们占据相同的高度
- python - 根据日期时间列上的条件对附加新列中的 Pandas DF 值求和
- javascript - 用于幻灯片 div 的 javascript
- git - Monorepo 具有通用库版本,仅更新一个项目
- java - Apache Beam Flatten 可迭代
- python - statsmodels中的分类交互?