【发布时间】:2019-06-03 12:29:07
【问题描述】:
我想将msg 的值赋给变量sample,但它无法在回调中访问sample。
import { Injectable } from '@angular/core';
import * as socketCluster from 'socketcluster-client';
@Injectable({
providedIn: 'root'
})
export class DatamanagerService {
socket = socketCluster.create({
port: 8000
});
sample:string;
constructor() {
}
connect() {
this.socket.on('connect', function () {
console.log('CONNECTED');
});
this.socket.on('random', function (msg) {
console.log('RANDOM: ' + msg);
this.sample = msg; // <- here
});
}
}
【问题讨论】:
标签: javascript angular typescript socketcluster