至尊寶的傳說(shuō)
2023-09-07 14:35:53
我想在訂閱塊中調(diào)用方法,但我得到“無(wú)法讀取未定義的屬性‘showMessageFromSocket’”。如何調(diào)用 showMessageFromSocket 方法?從角度來(lái)看這是可能的。export default class ConnectedChatroom extends Component< ConnectedChatroomProps, ConnectedChatroomState> { wsObj: CompatClient; constructor(props: Props, context: *) { super(props, context); this.configureSocketChannel('dassfa') } showMessageFromSocket(message) { console.log(message); //do something } configureSocketChannel(senderId: string) { let ws = Stomp.client("ws://localhost:8080/chat"); ws.connect({}, function (frame) { ws.subscribe("/topic/messages", function (message) { this.showMessageFromSocket(message); }); }, function (error) { console.log("STOMP error " + error); }); this.wsObj = ws; }
1 回答
素胚勾勒不出你
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
使用arrow函數(shù)來(lái)訪問(wèn)類成員,例如沒(méi)有自變量的類的變量或方法。
嘗試像下面這樣
configureSocketChannel(senderId: string) {
let ws = Stomp.client("ws://localhost:8080/chat");
ws.connect({}, (frame) => {
ws.subscribe("/topic/messages", (message) => {
this.showMessageFromSocket(message);
});
}, function (error) {
console.log("STOMP error " + error);
});
this.wsObj = ws;
}
添加回答
舉報(bào)
0/150
提交
取消
