我想将值分配给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
});
}
}