我想将 的值分配给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
});
}
}
繁星coding
相关分类