Angular 操作员'+';不能应用于类型
嗨,我有一个PayPal集成在Angular 5中。我使用此功能渲染Paypal按钮Angular 操作员'+';不能应用于类型,angular,typescript,angular5,Angular,Typescript,Angular5,嗨,我有一个PayPal集成在Angular 5中。我使用此功能渲染Paypal按钮 ngAfterViewChecked(): void { if (!this.addScript) { this.addPaypalScript().then(() => { this.abbonamenti.forEach((item, index) => { paypal.Button.render(this.payPalConfig +i
ngAfterViewChecked(): void {
if (!this.addScript) {
this.addPaypalScript().then(() => {
this.abbonamenti.forEach((item, index) => {
paypal.Button.render(this.payPalConfig +index, '#paypal-checkout-btn' + index);
this.paypalLoad = false;
})
})
}
}
这需要加载不同的PayPal config foreach按钮:
payPalConfig = {
env: 'sandbox',
client: {
sandbox: 'AfOCNI-QeZrhkX2lT5e4xY0S2KTfgoDarEXwk4mkK0ge4EoeW25VN5cg5ZlNRrdJrWUctHWBGGbP4d2V',
production: 'AUiobQL_EOnmPB4ytmb5ZZHbLZT4hXk7UZgMzGwkd8HFIR6qZ5qJZM3JOb91O4y5frw4197ygPyfbor0'
},
commit: true,
// payment() is called when the button is clicked
payment: function(data, actions) {
// Make a call to the REST api to create the payment
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: 40, currency: 'EUR' }
}
]
}
});
},
// onAuthorize() is called when the buyer approves the payment
onAuthorize: function(data, actions) {
// Make a call to the REST api to execute the payment
return actions.payment.execute().then(function() {
window.alert('Payment Complete!');
});
}
};
但某些串联或索引也不起作用。如果我尝试在函数名中添加+索引,我会在语法中出错。我搞不懂。我很沮丧 不能对对象应用
+
运算符。见下一行
paypal.Button.render(this.payPalConfig +index, '#paypal-checkout-btn' + index);
这里,上行中的this.payPalConfig
是导致此错误的对象
修理
您应该删除+
和索引
,只需传递直接对象
paypal.Button.render(this.payPalConfig, '#paypal-checkout-btn' + index);
编辑
配置应为
payPalConfig = new PayPalConfig(PayPalIntegrationType.ClientSideREST, PayPalEnvironment.Sandbox, {
commit: true,
client: {
sandbox: 'yourSandboxClientId',
},
button: {
label: 'paypal',
},
onPaymentComplete: (data, actions) => {
console.log('OnPaymentComplete');
},
onCancel: (data, actions) => {
console.log('OnCancel');
},
onError: (err) => {
console.log('OnError');
},
transactions: [{
amount: {
currency: 'USD',
total: 9
}
}]
});
参考-提供了答案中的修复程序。修复程序中有以下错误:src/app/main/content/pages/abbonamento/abbonamento.component.ts中的错误(22,10):错误TS2687:“payPalConfig”的所有声明必须具有相同的修饰符。src/app/main/content/pages/abbonamento/abbonamento.component.ts(43,3):错误TS2300:重复标识符“payPalConfig”。src/app/main/content/pages/abbonamento/abbonamento.component.ts(43,3):错误TS2403:后续变量声明必须具有相同的类型。变量“payPalConfig”的类型必须为“payPalConfig”,但此处的类型为“{env:string;client:{sandbox:string;production:string;};commit:boolean;payment:…”。src/app/main/content/pages/abbonamento/abbonamento.component.ts(43,3):错误TS2687:“payPalConfig”的所有声明必须具有相同的修饰符。看起来payPalConfig
结构与payPalConfig
类不匹配。请参考官方文档了解相同的内容。我添加了示例代码和参考。这可能会对您有所帮助。