SAP Spartacus注入自定义的CurrentProductService

时间:2022-07-25
本文章向大家介绍SAP Spartacus注入自定义的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));
  }

}

在构造函数里注入CurrentProductService. 在ngOnInit里,当this.product$发生变化时,打印product的内容:

运行时效果:每当打开一个产品明细页面,就能在控制台里观察到打印的产品json数据: