具体实现:
import { Component, OnInit } from '@angular/core';
import { ActiveCartService, Product } from '@spartacus/core';
import { CurrentProductService } from '@spartacus/storefront';
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
@Component({
selector: 'app-mycom',
templateUrl: './mycom.component.html',
styleUrls: ['./mycom.component.scss']
})
export class MycomComponent implements OnInit {
product$: Observable<Product> = this.currentProductService.getProduct().pipe(filter(product => !!product));
productName$ = this.product$.pipe(map(product => product.name));
constructor(private currentProductService: CurrentProductService, private cartService: ActiveCartService
) {
}
ngOnInit(): void {
this.product$.subscribe(product => console.log(product));
}
}
最后的效果: