1

如果想在 SAP Commerce Cloud 中向产品(模型/数据/DTO)添加新属性并想在 Spartacus 中访问它(使用 Spartacus ProductService),有什么方法?如何将属性引入 Spartacus 中的 Product 模型并使用来自后端的值填充它?

这个问题可以看作是一个通用问题,如何将此要求应用于所有模型并保持模型在后端和前端之间同步。

先感谢您。

4

1 回答 1

0

为了获取其他属性,您可以配置端点。有关更多信息,请参阅https://sap.github.io/cloud-commerce-spartacus-storefront-docs/connecting-to-other-systems/#configuring-endpoints。没有必要转换(规范化)数据,但您也可以这样做。这包含在相同的文档中。如果您需要适配第 3 方后端,您甚至可以用自定义适配器替换标准 OCC 适配器。

一旦从后端加载数据,它将存储在中央存储中,并由门面无限制地公开。但是,您可能希望增强默认类型,以便从类型安全中受益,而不是回退到any. 您可以使用以下方法执行此操作:

// intro custom typing
interface CustomProduct extends Product {
  customAttribute?: string;
}

// use typing for the observed data
product$: Observable<CustomProduct> = this.currentProductService.getProduct()
于 2020-01-29T15:26:44.080 回答