【发布时间】:2021-09-30 04:38:13
【问题描述】:
我正在尝试将 PayPal 按钮添加到现有的结帐页面。但是,我遇到了很多麻烦,因为我们的项目使用了 Vue.js 类组件,而我遇到的示例却没有(PayPal 官方文档不使用类组件:https://developer.paypal.com/docs/business/checkout/configure-payments/single-page-app/)。
我遇到了使用mounted()钩子将PayPal SDK脚本注入DOM的解决方法,并且我已经成功地使按钮出现,但是我无法付款的问题动态细节(总金额、商品描述等订单信息存储在 Vue 组件的状态中,我还没有找到将状态传递给 DOM 中的静态 JS 脚本的方法)。
我目前正在尝试调整 PayPal 官方文档以使用类组件,我的代码如下(去掉了与 PayPal 无关的部分):
索引.html:
<!DOCTYPE html>
<html class="no-js" lang="en" dir="ltr">
<head>
<script defer src="https://www.paypal.com/sdk/js?client-id=abc123&disable-funding=credit,card"></script>
</head>
<body>
<div id="app">
<!-- The shop component is put inside here -->
</div>
</body>
</html>
shop.vue:
<template>
<paypal-buttons :on-approve="onApprove" :create-order="createOrder" />
</template>
<script lang="ts" src="./shop.component.ts"></script>
shop.component.ts:
const PayPalButton = paypal.Buttons.driver("vue", window.Vue);
@Component({
components: {
PayPalButton,
},
})
export default class Shop extends Vue {
public total = '10.0'; // Will be dynamic
public mounted(): void {
...
}
public createOrder(data, actions) {
return actions.order.create({
purchase_units : [{
amount: {
value: total
}
}]
});
}
public onApprove(data, actions) {
return actions.order.capture().then(function (details) {
console.log(details)
})
}
}
此代码将成功构建,但由于出现错误,我无法实际打开页面。在浏览器控制台中,我看到以下错误:
TypeError: 无法读取未定义的属性“组件”
经过进一步调试,我发现paypal.Buttons.driver("vue", window.Vue); 行导致错误,这是因为paypal 未定义。我相当确定 index.html 中的 PayPal 脚本已正确加载,而且我也不认为这是由于缺少 npm 包或导入。我在网上找到的少数资源之一是:Vue PayPal implementation with vue-head, paypal not defined
不幸的是,这个链接的解决方案使用了mounted() 钩子,这是我之前尝试过的,它不能解决提供我想要的动态总数的问题。
有人有使用 PayPal SDK 和 Vue.js 类组件的经验吗?任何帮助将不胜感激!
【问题讨论】:
标签: javascript vue.js paypal vue-class-components