更新时间:2022-12-07 14:42:16
我使用了这样的解决方案:
I've used a solution like this:
加载外部脚本的方法
private loadExternalScript(scriptUrl: string) {
return new Promise((resolve, reject) => {
const scriptElement = document.createElement('script')
scriptElement.src = scriptUrl
scriptElement.onload = resolve
document.body.appendChild(scriptElement)
})
组件代码
ngAfterViewInit(): void {
this.loadExternalScript("https://www.paypalobjects.com/api/checkout.js").then(() => {
paypal.Button.render({
env: 'sandbox',
client: {
production: '',
sandbox: ''
},
commit: true,
payment: function (data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '1.00', currency: 'USD' }
}
]
}
})
},
onAuthorize: function(data, actions) {
return actions.payment.execute().then(function(payment) {
// TODO
})
}
}, '#paypal-button');
});
}
信用
Andrei Odri's answer here: script tag in angular2 template / hook when template dom is loaded