这是服务中的代码
getMsg(users_data: any): Subscription {
return this.socket.on('loadNewChat', async (data: any) => {
if(users_data.user1._id===data.sender_id && users_data.user2._id===data.reciever_id){
const container = document.getElementById('chat-container');
const element = document.createElement('ul');
if (this.msg !== data.message) {
element.innerHTML = data.message;
element.style.textAlign = 'right';
container!.appendChild(element);
}
}
});
}
字符串
这就是它在component.ts中的样子
private getMsgSubscription: Subscription = new Subscription();
ngOnInit(): void {
this.dialogdata = this.data
this.UserData.sender_id = this.dialogdata.user2._id
this.UserData.reciever_id = this.dialogdata.user1._id
this.getMsgSubscription = this.sockservice.getMsg(this.dialogdata)
this.sockservice.getChats(this.UserData)
}
ngOnDestroy(): void {
if (this.getMsgSubscription!== undefined) {
this.getMsgSubscription.unsubscribe();
}
}
型
我这样做是为了防止函数似乎被反复调用
的数据
但在这样做的时候,
chatview.component.ts:39 ERROR TypeError: this.getMsgSubscription.unsubscribe is not a function
at ChatviewComponent.ngOnDestroy (chatview.component.ts:58:31)
at executeOnDestroys (core.mjs:7359:32)
at cleanUpView (core.mjs:7267:9)
at destroyViewTree (core.mjs:7095:21)
at destroyLView (core.mjs:7245:9)
at RootViewRef.destroy (core.mjs:13738:9)
at ComponentRef.destroy (core.mjs:14195:23)
at DomPortalOutlet._disposeFn (portal.mjs:321:30)
at DomPortalOutlet._invokeDisposeFn (portal.mjs:217:18)
at DomPortalOutlet.detach (portal.mjs:201:14)
型
为什么会出现这种错误?如何纠正?
1条答案
按热度按时间txu3uszq1#
您无法取消订阅某个主题。
你可以使用这个:
字符串
若要关闭它,或在订阅时,请使用
型
把它转换成一个你可以取消订阅的观察对象。