Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ngrx库的无限循环问题_Javascript_Angular_Ngrx - Fatal编程技术网

Javascript 使用ngrx库的无限循环问题

Javascript 使用ngrx库的无限循环问题,javascript,angular,ngrx,Javascript,Angular,Ngrx,购物车服务.ts 从“/Tools”导入{Tools}; 从“/../\u store/state/Cart/Cart.state”导入{Cart,cartSelector}; 进口{ AddProduct, 移除产品, 添加多个, CartMsg, TypeMessageAlreadyCart, TypeEmptyCart, 地址, 更新产品 }来自“/../\u store/state/cart/cart.actions”; 从“/../\u store/models/cart item.m

购物车服务.ts

从“/Tools”导入{Tools};
从“/../\u store/state/Cart/Cart.state”导入{Cart,cartSelector};
进口{
AddProduct,
移除产品,
添加多个,
CartMsg,
TypeMessageAlreadyCart,
TypeEmptyCart,
地址,
更新产品
}来自“/../\u store/state/cart/cart.actions”;
从“/../\u store/models/cart item.model”导入{cartim,Variant};
从'@ngrx/Store'导入{Store};
从“@angular/core”导入{Injectable,OnDestroy};
从“../\u store/state/root/root.state”导入{RootState};
从'src/app/pages/_model/Product.model'导入{Product};
从“rxjs”导入{Subscription};
@注射的({
providedIn:'根',
})
导出类CartService实现OnDestroy{
产品:CartItem[];;
cartStoreId='cart';
idAddress=0;
订阅=新订阅();
建造师(
私家店,
私人工具:工具,
) {
//this.tools.storeInfo({},this.cartStoreId);//调试购物车的行
这个.recoverCartInfo();
const select=this.store.select(cartSelector).subscribe(
购物车=>{
this.products=JSON.parse(JSON.stringify(cart.products));
this.idAddress=cart.idAddress;
});
this.subscription.add(选择);
}
恩贡德斯特罗(){
this.subscription.unsubscripte();
}
storeCartInfo(){
常数存储={
产品:这个产品,,
idAddress:this.idAddress
};
this.tools.storeInfo(cartStored,this.cartStoreId);
}
updateCartPrices(idProduct:编号、变体){
设idx=this.tools.findProduct(idProduct,this.products);
如果(idx!=-1){
设total=0;
this.products[idx].variants.forEach(variant=>{
对于(让项目的变体){
if(variant.idVariant==item.ID\u variant){
variant.unitPrice=item.PRICE;
合计+=变型单价*变型数量;
继续;
}
}
})
此.products[idx].totalPrice=total;
}
//this.store.dispatch(newupdateproduct(this.products));
这个.storeCartInfo();
}
recoverCartInfo(){
const cart=this.tools.getInfo(this.cartStoreId);
if(购物车和(购物车产品)和(购物车产品长度>0)){
常量结果:购物车={
totalProducts:cart.products.length,
idAddress:cart.idAddress,
产品:购物车产品,
msgType:“”,
货币:this.tools.getInfo('currency\u default')
};
this.store.dispatch(newaddmultiple(result));
}
}
addProductToCart(产品:产品,变体?:变体[]){
if(此.changeProductInNot(产品、变体)){
this.store.dispatch(新添加产品(this.products));
这个.storeCartInfo();
返回true;
}否则{
this.store.dispatch(新类型messagealreadycart());
返回false;
}
}
ChangeProductIntranet(产品:产品,变体?:变体[]){
const index=this.tools.findProduct(product.ID\u product,this.products);
const attrAdded=(variants&&(variants.length>0))?this.mergeVariantsCart(product.ID_product,variants):[];
const totals=此计数器总计(attr added);
如果(索引==-1){
常数项={
idProduct:product.ID_产品,
参考:product.reference,
idProductImage:(product.ID\u product\u IMAGE)?product.ID\u product\u IMAGE:0,
名称:product.name,
totalQty:totals.tQty,
totalPrice:totals.tPrice,
变体:添加属性
};
本.产品.推送(项);
返回true;
}否则如果(变量&(变量.length>0)){
this.products[index].variants=attrAdded;
此.products[index].totalQty=totals.tQty;
此.products[index].totalPrice=totals.tPrice;
返回true;
}
返回false;
}
MergeVariantChart(idProduct:编号,变量:变量[]){
如果(this.products.length>0){
const index=this.tools.findProduct(idProduct,this.products);
如果(索引>=0){
const varInCart=this.products[index]。变量;
如果(varInCart.length>0){
variants.forEach(variant=>{
const idx=this.tools.findVariant(variant.idVariant,varInCart);
如果(变量数量>0){
如果(idx>=0){
varInCart[idx]。数量=variant.qty;
varInCart[idx]。单价=variant.unitPrice;
}否则{
可变推力(变型);
}
}如果(idx>=0),则为else{
可变截面拼接(idx,1);
}
});
}否则{
variants.forEach(variant=>{
如果(变量数量>0){
可变推力(变型);
}
});
}
返回varInCart;
}否则{
返回变量;
}
}否则{
返回变量;
}
}
总计(变量:变量[]){
让totalQty=0;
让totalPrice=0;
如果(!this.tools.isArrayEmpty(变体)){
variants.forEach(variant=>{
总数量+=(变量数量*1);
总价+=(变量数量*变量单价);
});
}
返回{
tQty:总数量,
价格:总价
};
}
addMultipleProductsToCart(产品:任意[]){
让加法=假;
products.forEach(p=>{
if(此.changeProductInNot(p.product,p.variants)){
添加=真;
}
});
如有(新增){
常量结果:购物车={
totalProducts:this.products.length,
idAddress:this.idAddress,
产品:这个产品,,
msgType:CartMsg.msgOk,
货币:this.tools.getInfo('currency\u default')
};
this.store.dispatch(newaddmultiple(result));
这个.storeCartInfo();
返回true;
}否则{
this.store.dispatch(新类型messagealreadycart());
}
返回fal