我目前正在尝试实现条纹到我的angular4 nodejs应用程序,但我有点卡住了,当我试图发送卡令牌通过我的服务处理与条带相关的请求到我的服务器。这是我的代码:条纹角度错误:未被捕获(承诺):TypeError:无法读取未定义的属性'stripeService'
stripe.component.html:
<form role="form" id="payment-form">
<div id="card-element"></div>
<div id="card-errors" role="alert"></div>
<button type="submit" (click)="addCard()">Submit Payment</button>
</form>
stripe.component.ts:
import {Component, OnInit} from "@angular/core";
import {WindowRef} from "../social/windowRef";
import {StripeService} from "./stripe.service";
@Component({
selector: "app-stripe",
templateUrl: './stripe.component.html',
styleUrls: ['./stripe.component.css']
})
export class StripeComponent implements OnInit {
elements: any;
stripe: any;
style: any;
card: any;
constructor(private stripeService: StripeService){}
ngOnInit() {
this.initCardElement();
}
initCardElement(){
this.stripe = WindowRef.get().Stripe("my-key");
this.elements = this.stripe.elements();
this.style =
{
base: {
color: '#32325d',
lineHeight: '24px',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
this.card = this.elements.create('card', {style: this.style});
this.card.mount('#card-element');
}
addCard() {
this.stripe.createToken(this.card)
.then(function (result) {
if (result.error) {
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
console.log(result.token);
this.stripeService.addCard(result.token)
.subscribe((response: Response) => {
console.log(response);
}
);
}
});
}
}
我设法得到条纹的卡令牌,但是当我试图打电话给我stripeService我得到这个错误:
Error: Uncaught (in promise): TypeError: Cannot read property 'stripeService' of undefined
我明白this.stripeService
不defin在这里编辑,但我不明白我可以如何解决这个问题。
有一个美好的一天:)
使用箭头功能,你失去了上下文。 功能(结果){/ *部分代码* /} 至 (结果)=> {/ *部分代码* /} –