如何删除parcelRequire在ionic中未使用peerjs定义

hk8txs48  于 2022-12-08  发布在  Ionic
关注(0)|答案(3)|浏览(165)

我正在建立一个视频聊天应用程序与离子5,我不知道如何正确使用它,我尝试了,但我不断得到这个错误的控制台

Uncaught ReferenceError: parcelRequire is not defined
    at push../node_modules/peerjs/dist/peerjs.min.js.parcelRequire.EgBh (peerjs.min.js:1)
    at Object../node_modules/peerjs/dist/peerjs.min.js

这是我的代码

import { Injectable } from '@angular/core';
import Peer from 'peerjs';

@Injectable({
    providedIn: 'root' 
}) 
export class WebrtcService {
    peer: Peer;
    myStream: MediaStream;
    myEl: HTMLMediaElement;
    partnerEl: HTMLMediaElement;
    options:any= {
        host: 'localhost',
        port: 9000,
        path: '/peerjs',
        debug: 3,
        config: {
            'iceServers': [{
                    url: 'stun:stun1.l.google.com:19302'
                },{
                    url: 'turn:numb.viagenie.ca',
                    credential: 'muazkh',
                    username: 'webrtc@live.com'
                }
            ]
        }
    }
    constructor() {}

    getMedia() {
        navigator.getUserMedia({ audio: true, video: true },stream=>this.handleSuccess(stream),
        (error) =>this.handleError(error));
    }

    async init(userId: string, myEl: HTMLMediaElement, partnerEl: HTMLMediaElement) {
        this.myEl = myEl;
        this.partnerEl = partnerEl;
        try {
            this.getMedia();
        } catch (e) {
            this.handleError(e);
        }
        await this.createPeer(userId);
    }

    async createPeer(userId: string) {
        this.peer = new Peer(userId, this.options);
        this.peer.on('open', ()=>this.wait())
    }

    call(partnerId: string) {
        const call = this.peer.call(partnerId, this.myStream);
        call.on('stream', (stream)=>this.partnerEl.srcObject = stream);
    }

    wait() {
        this.peer.on('call', (call) => {
            call.answer(this.myStream);
            call.on('stream', (stream) =>this.partnerEl.srcObject = stream);
        });
    }

注解大部分代码以找出错误来自哪里,我注意到错误发生在运行this.peer = new Peer(userId, this.options);时。
我还注意到,如果我应该在其他页面运行它将工作,但如果它正在运行在一个app.component.ts,它会给予我的错误,我严重需要上述代码是在app.component.ts

x8diyxa7

x8diyxa71#

试试这个:

<script>
    var parcelRequire;
</script>

(From(第一次)

iyfamqjs

iyfamqjs2#

它在第2版包中得到解决,只需安装包

npm i -D parcel
iecba09b

iecba09b3#

如果您的脚本使用type="module"-请删除它。

相关问题