【发布时间】:2020-12-03 20:02:57
【问题描述】:
我正在创建与支付服务的集成。 支付服务为我提供了一个带有脚本标签的表单。
我的问题是Insert a script tag inside template Vue的延续
支付服务结账表格:
<form action="http://localhost:8081/api/v1/payment/" method="POST">
<script
src="https://www.mercadopago.com.br/integrations/v1/web-tokenize-checkout.js"
data-public-key="KEY"
data-transaction-amount="14.90">
</script>
</form>
我可以在 vuejs 的“mounted()”上做下一个:
<form ref="myform">
...
</form>
mounted() {
let foo = document.createElement('script');
foo.setAttribute("src","https://www.mercadopago.com.br/integrations/v1/web-tokenize-checkout.js");
foo.setAttribute("data-transaction-amount", this.newAmount)
this.$refs.myform.appendChild(foo);
}
但是,我的问题是在安装视图之后。用户可以更改“data-transaction-amount”。
为了解决它,我尝试了:
data:()=>({
newAmount:0
})
watch: {
newAmount() {
this.modifyScript();
},
},
methods: {
modifyScript() {
let scripts = document.getElementsByTagName("script");
for (let i = 0; i < scripts.length; i++) {
let script = scripts[i];
if (script.getAttribute("src") == 'https://www.mercadopago.com.br/integrations/v1/web-tokenize-checkout.js') {
// we've got a match
script.setAttribute("data-transaction-amount", this.newAmount);
}
}
},
“data-transaction-amount”正在更改为新值,但支付服务的窗口结帐显示“mounted()”中使用的原始值。
【问题讨论】:
-
你试过
scripts[i].setAttribute("data-transaction-amount", this.newAmount);吗? -
这听起来像是您需要向该脚本的提供者提出的问题。询问他们如何动态更新交易金额
-
是的,我做到了。正如我所说,这不是问题。在 chrome 控制台调试器中,我可以看到它正在工作。问题是窗口支付服务显示的是“挂载”周期中使用的旧值,而不是新值。
-
@Phil 我认为这不是一个选择。提供者是一个大玩家“mercadopago”。我猜他们并不担心。
标签: javascript vue.js vuejs2