一般我们使用tim都是在某个页面中初始化,使用vue时,多次进入这个页面就会造成Tim对象多次初始化并触发事件。tim其实提供了destroy方法,但是这个方法没有在文档中。
使用方法:
beforeDestroy() {
clearInterval(this.intervalId)
this.tim.destroy()
// this.tim.logout().then(resp => {
// console.log(resp.data); // 登出成功
// this.tim.offEventHandler(TIM.EVENT.ERROR, this.onError)
// this.tim.offEventHandler(TIM.EVENT.SDK_NOT_READY, this.onSdkNotReady)
// this.tim.offEventHandler(TIM.EVENT.SDK_READY, this.onSdkReady)
// this.tim.offEventHandler(TIM.EVENT.MESSAGE_RECEIVED, this.onMessageReceived)
// this.tim.offEventHandler(TIM.EVENT.KICKED_OUT, this.onKickedOut)
// }).catch(imError => {
// console.warn('logout error:', imError);
// });
this.tim = null
},
注意:调用了destroy()就不能先调用或者后调用logout,因为destroy()会自动logout()