vue.js - 如何在 Vue 中从外部回调中设置数据?
问题描述
我在组件中包含了一些外部 SDK:
mounted() {
// Load external Script
let externalScript = document.createElement('script')
externalScript.setAttribute('src', 'https://x.klarnacdn.net/kp/lib/v1/api.js')
document.head.appendChild(externalScript)
...
后来我收到这样的回调:
authorizeKlarna: function(){
Klarna.Payments.authorize({
payment_method_category: "pay_later"
}, function(res) {
console.debug(res);
if(res.approved == true){
this.token = res.authorization_token
}
})
},
但是this.token
不会更新,因为我超出了范围。有什么简单的方法可以从这个外部回调中访问我的 vue 组件的数据吗?
解决方案
尝试改变你function(res)
的箭头功能(res) =>
authorizeKlarna: function(){
Klarna.Payments.authorize(
{ payment_method_category: "pay_later" },
(res) => {
console.debug(res);
if(res.approved == true){
this.token = res.authorization_token
}
}
)
},
推荐阅读
- c++ - C ++:用作逻辑值时如何返回对象的设置状态
- csv - 在 Mautic 3.0.0 中导入联系信息时出现问题
- php - 无法通过 drush 从 Drupal 容器连接到 MYSQL 容器
- node.js - 无法将 GRPC 微服务与客户端连接 - 在 k8s 上嵌套 JS 节点
- python - 几个屏幕“重叠”pygame
- math - 为半浮点优化 GLSL
- vue.js - 更新 FormulateForm 中的整数不起作用
- x86 - 初始加载后如何更新 GDT 条目?
- r - 将ggplot图例更改为离散而不是连续
- java - Invokedynamic 工厂将 lambdas 创建为单例?