你可以监听 WebSocket 对象的事件,然后在事件处理器里主动修改 Vue 组件中的属性,例如:
export default {
data() {
return {
wsReadyState: null
}
},
mounted() {
this.ws = new WebSocket(`ws://${process.env.VUE_APP_API_HOST}:9501`);
this.ws.addEventListener('open', () => {
this.ws.send('Hello Server!');
this.wsReadyState = this.ws.readyState;
});
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…