【发布时间】:2021-01-20 21:12:10
【问题描述】:
我正在尝试使用 Angular 10 使用 asp.net core 3 api 检查数据是否存在,但只有在返回和发布时才放入,否则在 TS 代码下不起作用
handleAddToCart() {
let checking = this.CartService.getCartItemByItemId(this.productItem.itemLookupCode, this.cartForm.value.weight, this.productItem.storeId)
.subscribe(c=>{
return c;
});
if (checking ) {
const updateCartPost = {
itemID: this.productItem.itemID,
itemLookupCode: this.productItem.itemLookupCode,
categoryID: this.productItem.categoryID,
departmentID: this.productItem.departmentID,
itemDescription: this.productItem.itemDescription,
subDescription3: this.productItem.subDescription3,
quantity: this.cartForm.value.qty,
weight: this.cartForm.value.weight,
snapShotPrice: this.productItem.snapShotPrice,
storeId: this.productItem.storeId,
barcode: this.productItem.barcode,
email: localStorage.getItem('email'),
itemImage: ''
}
this.CartService.updateCartItem(updateCartPost).subscribe(u => {
this.msg.sendMsg(this.AddToCart(this.cartForm.value.weight));
this.toaster.success('Added To Cart');
console.log('sssssr');
});
}
else{
this.CartService.addProductToCart(this.AddToCart(this.cartForm.value.weight)).subscribe(() => {
this.msg.sendMsg(this.AddToCart(this.cartForm.value.weight));
this.toaster.success('Added To Cart');
console.log(this.AddToCart(this.cartForm.value.weight));
});
}
}
谁能帮帮我?
我需要对 if 和 else 使用条件,但它只返回 if 并且没有看到 else。
【问题讨论】:
-
你不能从这样的 observable 返回。查看
checking的类型,其实是Subscription。查看带有可观察对象的管道,特别是switchMap应该在这里使用,因为您正在链接 API 调用。
标签: typescript angular9 angular10