无法从依赖Websocket的共享服务获取数据

时间:2018-11-24 16:20:30

标签: angular websocket socket.io

在组件加载时,我调用websocket函数,要求服务器提供字符列表

菜单组件->

constructor(private router: Router, public shared: SharedService, private wss: WebsocketService) {
    this.wss.emitGetCharacters();
}

Websocket服务->

// Get Character List
public emitGetCharacters(): any {
    if (!this.socket) {
        this.initSocket();
        this.socket.emit('getCharacters', this.idUser);
    } else {
        this.socket.emit('getCharacters', this.idUser);
    }
}

然后服务器在Websocket服务上正确返回字符,该服务使用共享服务存储字符列表

    this.socket.on('characterList', (res) => {
        this.shared.setCharacters(res);
    });

然后在菜单组件ngOnInit上,尝试使用共享功能获取字符

public getCharacters() {
    return this.characters;
}

这将返回未定义的

我知道这与Observables和RxJS有关,但是我一直在阅读,我不明白如何在这种情况下实现它

0 个答案:

没有答案