SAP Spartacus injects a custom CurrentProductService

import { Component, OnInit } from '@angular/core';
import { ActiveCartService, Product } from '@spartacus/core';
import { CurrentProductService } from '@spartacus/storefront';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-mycom',
  templateUrl: './mycom.component.html',
  styleUrls: ['./mycom.component.scss']
})
export class MycomComponent implements OnInit {

  product$: Observable<Product> = this.currentProductService.getProduct();

  constructor(private currentProductService: CurrentProductService, private cartService: ActiveCartService
  ) {

  }

  ngOnInit(): void {
    this.product$.subscribe(product => console.log(product));
  }

}

Inject CurrentProductService into the constructor. In ngOnInit, when this.product$ changes, print the content of product:

Run-time effect: Whenever you open a product detail page, you can observe the printed product json data in the console:


To get more original articles by Jerry, please follow the public account "Wang Zixi":

Guess you like

Origin blog.csdn.net/i042416/article/details/108654715