亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我無法在訂閱中調用方法 - 無法讀取未定義的屬性“showMessageFromSocket”

為什么我無法在訂閱中調用方法 - 無法讀取未定義的屬性“showMessageFromSocket”

至尊寶的傳說 2023-09-07 14:35:53
我想在訂閱塊中調用方法,但我得到“無法讀取未定義的屬性‘showMessageFromSocket’”。如何調用 showMessageFromSocket 方法?從角度來看這是可能的。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貢獻1827條經驗 獲得超9個贊

使用arrow函數來訪問類成員,例如沒有自變量的類的變量或方法。


嘗試像下面這樣


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;

}


查看完整回答
反對 回復 2023-09-07
  • 1 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號