我目前正在嘗試實現條紋到我的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在這裏編輯,但我不明白我可以如何解決這個問題。
有一個美好的一天:)
使用箭頭功能,你失去了上下文。 功能(結果){/ *部分代碼* /} 至 (結果)=> {/ *部分代碼* /} –