前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SAP Spartacus的Component映射

SAP Spartacus的Component映射

原创
作者头像
Jerry Wang
修改2020-11-23 10:29:32
5060
修改2020-11-23 10:29:32
举报

Spartacus默认的购物车界面:

https://github.com/SAP/spartacus-bootcamp/blob/master/sparta0/src/app/components/cart.component.ts

新建一个CartComponent,对Spartacus标准的CartDetailsComponent进行扩展:

代码语言:txt
复制
import { Component } from '@angular/core';
import { CartDetailsComponent } from '@spartacus/storefront';

@Component({
  selector: 'app-cart',
  templateUrl: './cart.component.html',
  styleUrls: ['./cart.component.scss']
})
export class CartComponent extends CartDetailsComponent {

  onChange(entryNumber, event) {
    this.activeCartService.updateEntry(entryNumber, event.target.value);
  }

}

界面元素如下:

代码语言:txt
复制
<ng-container *ngIf="cart$ | async as cart">
  <ng-container *ngIf="entries$ | async as entries">
    <div *ngIf="cart.totalItems > 0" class="cart-details-wrapper">
      <cx-spinner *ngIf="!(cartLoaded$ | async)"></cx-spinner>
      <table
        class="table table-striped"
        [class.loading]="!(cartLoaded$ | async)"
      >
        <thead>
          <tr>
            <th scope="col">#</th>
            <th scope="col">Image</th>
            <th scope="col">Code</th>
            <th scope="col">Name</th>
            <th scope="col">Manufacturer</th>
            <th scope="col">Price</th>
            <th scope="col">Quantity</th>
            <th scope="col">Total</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let entry of entries; index as i">
            <td>{{ i + 1 }}</td>
            <td>
              <a
                [routerLink]="
                  { cxRoute: 'product', params: entry.product } | cxUrl
                "
              >
                <cx-media
                  [container]="entry.product.images?.PRIMARY"
                  format="thumbnail"
                ></cx-media>
              </a>
            </td>
            <td>{{ entry.product.code }}</td>
            <td>{{ entry.product.name }}</td>
            <td>{{ entry.product.manufacturer }}</td>
            <td>{{ entry.basePrice?.formattedValue }}</td>
            <td>
              <input
                type="number"
                [value]="entry.quantity"
                (change)="onChange(entry.entryNumber, $event)"
              />
            </td>
            <td>{{ entry.totalPrice.formattedValue }}</td>
          </tr>
          <tr>
            <td colspan="7"></td>
            <td class="table-active">
              <strong>{{ cart.totalPriceWithTax.formattedValue }}</strong>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </ng-container>
</ng-container>

在NgModule里,使用ConfigModule将CartComponent对应的Angular Component替换成我们自定义的CartComponent:

代码语言:txt
复制
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CartComponent } from './cart.component';
import { ConfigModule, CmsConfig, UrlModule, FeaturesConfigModule, I18nModule } from '@spartacus/core';
import { CartSharedModule, CartCouponModule, PromotionsModule, MediaModule, SpinnerModule } from '@spartacus/storefront';
import { RouterModule } from '@angular/router';
import { FormsModule } from '@angular/forms';



@NgModule({
  declarations: [CartComponent],
  imports: [

    CartSharedModule,
    CommonModule,
    CartCouponModule,
    RouterModule,
    MediaModule,
    UrlModule,
    PromotionsModule,
    FeaturesConfigModule,
    I18nModule,
    FormsModule,
    SpinnerModule,

    ConfigModule.withConfig({
      cmsComponents: {
        CartComponent: {
          component: CartComponent,
        },
      }
    } as CmsConfig)
  ],
  entryComponents: [CartComponent]
})
export class ComponentsModule { }

运行时效果:

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档