【发布时间】:2020-08-19 08:42:45
【问题描述】:
我几乎在一周前就开始使用 Angular。为了让事情变得简单,我正在尝试实施贝宝结帐。
只要我只通过总金额,结帐就可以正常工作。但我想将几个项目传递给贝宝。
我是这样尝试的:
import {
Component,
OnInit,
ViewChild,
Input,
ElementRef,
Output,
EventEmitter
} from "@angular/core";
declare var paypal;
@Component({
selector: "app-payme",
templateUrl: "./payme.component.html",
styleUrls: ["./payme.component.scss"]
})
export class PaymeComponent implements OnInit {
@ViewChild("paypal", { static: true }) paypalElement: ElementRef;
@Input() total: number;
@Input() cart: [];
//payCart: [];
@Output() isPaidFor: EventEmitter<boolean> = new EventEmitter();
paidFor = false;
constructor() {}
cartInConsole() {
console.log("PayPalTotal", this.total);
}
declareItem() {
let paypalItem = [];
for (let i =0 ; i< this.cart.length; i++) {
let currency = "EUR";
let quantity = this.cart[i].selectedSize;
let name = this.cart[i].name;
let amount = this.cart[i].selectedPrice;
console.log("DeclareFunction rennt");
//let pushItem = {"quantity": quantity};
paypalItem.push({"unit_amount": {"currency_code": "EUR","value": currency, "amount": amount},"quantity": quantity,"name": name});
console.log("Declare", paypalItem)
return paypalItem
}
console.log("StackOverFow", paypalItem )
};
ngOnInit() {
console.log("PayPalCart", this.cart);
this.declareItem();
paypal
.Buttons({
createOrder: (data, actions) => {
return actions.order.create(
{
purchase_units: [{
amount: {
currency_code: 'EUR',
value: this.total,
}
},
items: this.paypalItem
}]
},
onApprove: async (data, actions) => {
const order = await actions.order.capture();
this.paidFor = true;
this.isPaidFor.emit(this.paidFor);
console.log("Order", order);
}
})
.render(this.paypalElement.nativeElement);
}
}
但我得到的回应是:未定义项目。
我的 paypalItem 的 console.log 看起来不错 - 它似乎是一个对象数组,就像它应该的那样。
但是我如何让它将该对象传递给项目或项目?
顺便问一下:是哪一个?物品或物品?几个小时的谷歌搜索得到所有不同的答案,让你怀疑任何事情:-)
提前非常感谢!
更新
我尝试声明 paypalItem,然后在 declareItem 函数中进行设置。不幸的是,结果相同...
现在的代码如下所示:
import {
Component,
OnInit,
ViewChild,
Input,
ElementRef,
Output,
EventEmitter
} from "@angular/core";
declare var paypal;
@Component({
selector: "app-payme",
templateUrl: "./payme.component.html",
styleUrls: ["./payme.component.scss"]
})
export class PaymeComponent implements OnInit {
@ViewChild("paypal", { static: true }) paypalElement: ElementRef;
@Input() total: number;
@Input() cart: [];
paypalItem: [];
//payCart: [];
@Output() isPaidFor: EventEmitter<boolean> = new EventEmitter();
paidFor = false;
constructor() {}
cartInConsole() {
console.log("PayPalTotal", this.total);
}
declareItem() {
let paypalItem = [];
for (let i =0 ; i< this.cart.length; i++) {
let currency = "EUR";
let quantity = this.cart[i].selectedSize;
let name = this.cart[i].name;
let amount = this.cart[i].selectedPrice;
console.log("DeclareFunction rennt");
//let pushItem = {"quantity": quantity};
paypalItem.push({"unit_amount": {"currency_code": "EUR","value": currency, "amount": amount},"quantity": quantity,"name": name});
console.log("Declare", paypalItem)
this.paypalItem = paypalItem;
}
console.log("StackOverFow", paypalItem )
};
ngOnInit() {
console.log("PayPalCart", this.cart);
this.declareItem();
paypal
.Buttons({
createOrder: (data, actions) => {
return actions.order.create(
{
purchase_units: [{
amount: {
currency_code: 'EUR',
value: this.total,
}
},
items: this.paypalItem
}]
},
onApprove: async (data, actions) => {
const order = await actions.order.capture();
this.paidFor = true;
this.isPaidFor.emit(this.paidFor);
console.log("Order", order);
}
})
.render(this.paypalElement.nativeElement);
}
}
还有其他想法吗?
另一个更新:
完整代码在codesandbox
【问题讨论】:
-
配合您尝试访问 this.paypalItem。它从来没有在类级别的某个地方声明过,那么你想如何获得对它的引用?
标签: javascript angular paypal