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数据:


网友评论