为Spartacus产品字段添加额外的属性,可以通过在Spartacus storefront的后端代码中定义新的属性,并通过GraphQL API和数据模型将其暴露给Spartacus storefront。以下是实现该功能的步骤:
src\app\src\product\services\product.service.ts
在ProductService中定义新属性,如下所示:
import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { Product } from '@spartacus/core';
@Injectable({ providedIn: 'root' }) export class ProductService { constructor() {}
get(productCode: string): Observable
getBySlug(slug: string): Observable
getByCode(code: string): Observable
getAdditionalProperties(productCode: string): Observable
src\app\src\product\graphql\product.graphql
在Product GraphQL中定义新字段,如下所示:
query GetProduct($code: String!) { product(code: $code) { code name description images { ...ImageMeta } price { ...PriceBase ... on Price { formattedValue } } ${productAdditionalFields} //New field } }
src\app\src\product\occ-models\occ.models.ts
定义新的字段数据结构,如下所示:
export interface ProductAdditionalFields { additionalField1: string; additionalField2: number; }
export interface Product extends Occ.Product { ${product