Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度11:错误类型错误:“;。。。不是函数”;。但实际上是这样_Javascript_Angular_Typeerror - Fatal编程技术网

Javascript 角度11:错误类型错误:“;。。。不是函数”;。但实际上是这样

Javascript 角度11:错误类型错误:“;。。。不是函数”;。但实际上是这样,javascript,angular,typeerror,Javascript,Angular,Typeerror,我正在开发基于webrtc的音频/视频会议应用程序。为此,我在Angular 11中创建了前端和后端节点(express server)。所以,我用vanilla JS、HTML、CSS创建了这个应用程序,它运行正常。但当我试图将前端转换为Angular应用程序时,问题就出现了 应用程序的工作过程如下,首先我创建房间,然后我会得到一个roomId,我将它发送给另一个人,他使用该roomId加入房间进行聊天 当另一个人加入房间时,onRoomJoined()在我这边执行 async onRoomJ

我正在开发基于webrtc的音频/视频会议应用程序。为此,我在Angular 11中创建了前端和后端节点(express server)。所以,我用vanilla JS、HTML、CSS创建了这个应用程序,它运行正常。但当我试图将前端转换为Angular应用程序时,问题就出现了

应用程序的工作过程如下,首先我创建房间,然后我会得到一个
roomId
,我将它发送给另一个人,他使用该
roomId
加入房间进行聊天

当另一个人加入房间时,
onRoomJoined()
在我这边执行

async onRoomJoined(数据){
等待此。设置连接(数据['client-id',数据['client-name']);
this.socket.emit('send-metadata',{'room id':this.roomId,'client name':this.clientName,
“客户端id”:this.clientId,“对等id”:数据['client-id']});
}
现在该函数进一步调用
setUpConnection()
函数,如下所示:


异步设置连接(peerId、peerName、initiateCall=false){
const videoElement=this.getVideoElement(peerId,0,peerName);
videoElement.autoplay=true;
videoElement.playsInline=true;
this.peerConnections[peerId]={“对等名称”:peerName,“pc”:新的RTCPeerConnection(iceServers)};
this.peerConnections[peerId].pc.ontrack=(track)=>{this.setRemoteStream(track,peerId);};
这个.addLocalStreamTracks(peerId);
this.peerConnections[peerId].pc.onicecandidate=(iceCandidate)=>{this.gathereicecandidates(iceCandidate,peerId);};
this.peerConnections[peerId].pc.oniceconnectionstatechange=(事件)=>{this.checkPeerDisconnection(事件,peerId);};
如果(initiateCall==真){
等待这个。createOffer(peerId);
}
}
以下是我在浏览器控制台(room creator)中遇到的错误

ERROR ERROR:Uncaught(承诺中):TypeError:this.setUpConnection不是函数
TypeError:此.setUpConnection不是函数
在插座上。(home.component.ts:440)
在Generator.next()处
在tslib.es6.js:76
在新的ZoneAwarePromise(zone evergreen.js:960)
在等待者(tslib.es6.js:72)
在Socket.onRoomJoined(home.component.ts:438)
位于Socket.push.cpc2.Emitter.emit(index.js:145)
在Socket.emit(typed events.js:46)
在Socket.emitEvent上(Socket.js:263)
位于Socket.onevent(Socket.js:250)
at resolvePromise(zone evergreen.js:798)
在新的ZoneAwarePromise(zone evergreen.js:963)
在等待者(tslib.es6.js:72)
在Socket.onRoomJoined(home.component.ts:438)
位于Socket.push.cpc2.Emitter.emit(index.js:145)
在Socket.emit(typed events.js:46)
在Socket.emitEvent上(Socket.js:263)
位于Socket.onevent(Socket.js:250)
位于Socket.onpacket(Socket.js:214)
位于Manager.push.cpc2.Emitter.emit(index.js:145)
这里,上述两个函数
onRoomJoined()
setUpConnection()
都位于
home.component.ts
文件中。我仍然得到
类型错误:this.setUpConnection不是一个函数

这怎么可能


感谢您的帮助,谢谢

如果onroomjoin函数作为回调函数调用,如onontrack=onroomjoin,则
不再是角度分量。您可以通过在onRoomJoined中将此函数或setUpConnection函数作为参数传递,并在内部使用/调用它来解决此问题。

我认为您误解了“this”关键字事件处理程序中的“连接”将引用套接字实例,而不是组件本身,因此它将无法在其附近找到setUpConnection()函数。您需要做的不是使用回调函数,而是尝试在事件处理程序中使用arrow函数。例如,如果您已完成以下操作:

socket.on('some-event', onRoomJoined);

function onRoomJoined(data) {
    ...
    ...
    ...
}
你应该做的是:

socket.on('some-event', (data) => {
    ...
    ...
    ...
});

现在在arrow函数中,关键字“this”将引用您的组件,您可以毫不费力地调用setUpConnection()函数。

请考虑
this
的值可能不是您所认为的值。.将“this”绑定到OnRoomJoint调用,或使用arrow函数调用它,例如onRoomJoint.bind(this)或(数据)=>onRoomJoined(数据)哇!!大粉丝先生