我是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([])));
}