typescript - 无法访问变量
问题描述
pay(amount){
var handler = (<any>window).StripeCheckout.configure({
key: 'public stripe key',
locale: 'auto',
token: function (token: any) {
// You can access the token ID with `token.id`.
// Get the token ID to your server-side code for use.
this.stripeToken = token.id;
}
});
console.log('token is ' + this.stripeToken);
handler.open({
name: 'Demo Site',
description: '2 widgets',
amount: amount * 100
});
console.log(this.stripeToken + ' again')
}
由于某种原因,我无法访问令牌函数之外的 this.stripeToken 变量。当我尝试使用 'token is' + stripeToken 或 this.stripetoken + 'again' 来 console.log() 时,我只是得到一个未定义的变量。条带变量声明如下: public stripeToken: any; 在 component.ts 页面的顶部,这是其中的一部分。请帮忙。
解决方案
由于某种原因,我无法访问令牌函数之外的 this.stripeToken 变量。
那是因为它还不存在。将来某个时候会异步调用令牌函数,但您的日志语句会立即发生。如果您有需要使用令牌的代码,则应将其放入回调函数中。
var handler = (<any>window).StripeCheckout.configure({
key: 'pk_test_51HX4yUDEnGCSjwlXYRIFs3Wj9fFXw8DW7kLUacKFKPIcC0P96E6C4I9kVku5brUOGR33O2KKH6NkfIawr3oo11eU00eL9q8lAk',
locale: 'auto',
token: function (token: any) {
// Put your code that uses the token here
}
});
推荐阅读
- apache-kafka - 如何使用 KStreams API (Java) 添加标头
- ssl - 无法在大使上启用 TLS
- vue.js - vue-jalali-moment 过滤器在 v-for 上无法正常工作
- git - Azure DevOps Pipeline (CI) 中的 Git 问题
- wordpress - 如何更改这个罕见的自定义帖子类型存档 URL
- c# - 当 catch 未捕获时未执行 try
- java - 有没有办法设置标签内联代码的颜色?
- java - 没有为预期的 URL 调用 Spring Controller
- spring-batch - 由一个春季批处理作业准备的文件不能被其他人访问以进行删除
- swift - 如何正确地将日期转换为 NSDate?(在展开 Optional 值时意外发现 nil)