我正在尝试在jQuery函数内调用onPaymentStatus()函数。但是我无法使用“这个”范围
this.braintree.client.create({
authorization: 'client_token'},
(err, client) => {
client.request({
endpoint: 'payment_methods/credit_cards',
method: 'post',
data: data
}, function (requestErr, response) {
if (requestErr) { throw new Error(requestErr); }
console.log('Got nonce:', response.creditCards[0].nonce);
this.onPaymentStatus(response.creditCards[0].nonce); //
});
});
onPaymentStatus (nonce) {
console.log(nonce);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误ERROR TypeError:无法读取null的属性'onPaymentStatus'
我使用'rxjs-websockets'来连接websockets.但在一定时间(约2分钟)后,连接关闭.在手动关闭之前,如何保持连接.
这是我使用的代码片段
constructor(private socketService: WebSocketService) {}
this.socketService.connect('/endpoint');
this.socketSubscription = this.socketService.messages
.subscribe(result => {
// perform operation
});
Run Code Online (Sandbox Code Playgroud)
这是WebSocketService
import {Injectable} from '@angular/core';
import {QueueingSubject} from 'queueing-subject';
import {Observable} from 'rxjs/Observable';
import websocketConnect from 'rxjs-websockets';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/retryWhen';
import 'rxjs/add/operator/delay';
@Injectable()
export class WebSocketService {
private inputStream: QueueingSubject<string>;
public messages: Observable<string>;
constructor() {
}
public connect(socketUrl) {
this.messages = websocketConnect(
socketUrl,
this.inputStream = new QueueingSubject<string>()
).messages.retryWhen(errors => errors.delay(1000))
.map(message => JSON.parse(message))
.share();
}
public send(message: string): void {
this.inputStream.next(message);
} …
Run Code Online (Sandbox Code Playgroud) 我有一个在组件A中编写的websocket逻辑,如下所示.
this.socketService.connect('/socket_url');
this.statusSubscription = this.socketService.messages
.subscribe(result => {
if (result !== 'pong') {
// update Component B with the response obtained
}
});
Run Code Online (Sandbox Code Playgroud)
我想知道每当我在旅途中收到websocket活动时如何更新组件B.