我是 angular2 ngrx/store 和 ngrx/effects 的新手。我已经整理了一个应用程序,我将物品放入商店。我无法正确结帐。我的商店由标题信息(如运输信息)以及作为数组的项目信息组成。像这样:
export interface State {
entities:CatalogDetail[];
cart: Cart;
};
我的行动:
export const ActionTypes = {
CHECKOUT_REQUEST: 'Checkout',
}
export class CheckoutAction implements Action {
type = ActionTypes.CHECKOUT_REQUEST;
constructor (public payload:CatalogDetail[]) { }
}
我需要创建一个包含购物车运输信息的标题记录,然后在收到订单号后创建目录信息。我必须为每个功能使用两个休息服务。我能够创建订单。
通过做这个:
export class CheckoutComponent implements OnInit {
public collections:Observable<CatalogDetail[]>;
public collectionsCart:Observable<Cart>;
actions$ = new Subject<Action>();
errorMessage:string = " ";
orderNumber:Observable<number>;
details:CatalogDetail[];
public final = false;
constructor(private route:ActivatedRoute,
private router:Router,
private store: Store<fromRoot.State>,
private orderService:OrderService) {
this.collections = store.let(fromRoot.getCollectionEntities);
this.collectionsCart = store.let(fromRoot.getCollectionCart);
}
ngOnInit(): void {
this.employee = this.employeeService.getEmployeeInfo('0R5821');
this.final = false;
}
checkout(inCart: Cart) {
this.final = true;
console.log("inCheckOutMethod: " + inCart.shipToName);
this.store.dispatch(new collection.UpdateCartAction(inCart));
//create the order and return the order number
this.orderNumber = this.orderService.createOrders(inCart);
this.store.dispatch(new collection.CheckoutSuccessAction([]));
}
}
我写的效果只有在有效负载中有一个 catalogDetail 时才有效:
@Injectable()
export class ShopEffects {
constructor(private actions$: Actions, private orderService:OrderService, private store: Store<fromRoot.State>) { }
@Effect()
checkOut$ = this.actions$
.ofType(cart.ActionTypes.CHECKOUT_REQUEST)
.map(toPayload)
.switchMap(payload => this.orderService.createOrderItem(payload, "0R5821", 33439))
.map(payload => this.store.dispatch(new collectionActions.CheckoutSuccessAction([])));
}