[英]Rxjs Subject/Observable subscribe Array of list
I have one easy question. 我有一个简单的问题。 I have service
DataStorageServiceService
and in this service I have shopCartProtudct = new Subject<ProductModel[]>();
我有
DataStorageServiceService
服务,在此服务中,我有shopCartProtudct = new Subject<ProductModel[]>();
. 。 I have component
SingleProductComponent
and from this component I want sent data sent data in to shopCartProtudct
but like array, I want to interact with interact shopCartProtudct
like basic array, than means, I want to store data and delete. 我有组件
SingleProductComponent
,我想从该组件中发送数据,并将数据发送到shopCartProtudct
但是像数组一样,我想与交互式shopCartProtudct
像基本数组进行交互,这意味着我要存储数据并删除。 This is posible? 这可以吗?
You are likely looking for a Service implementation like this: Note: Untested code, consider it a pseudo code almost. 您可能正在寻找像这样的Service实现:注意:未经测试的代码,几乎将其视为伪代码。
export class DataStorageService {
//A subject that you can subscribe on
shopCardProduct = new Subject<ProductModel[]>();
//array behind the products in the cart
products : ProductModel[] = new Array<ProductModel>() ;
constructor(){
this.shopCardProduct = new BehaviorSubject<ProductModel>(new Array<ProductModel>())
}
shop(product: ProductModel){
this.products.push(product);
this.shopCardProduct.next(this.products);
}
getCart(): Subject<ProductModel[]> {
return this.shopCardProduct;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.