angular - 如何删除在ionic中peerjs没有定义parcelRequire

标签 angular ionic-framework peerjs

我正在使用 ionic 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: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7403111606001734181d02115a171b19" rel="noreferrer noopener nofollow">[email protected]</a>'
                }
            ]
        }
    }
    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 中运行,它会给我这个错误,我真的需要上面的代码在应用程序中.component.ts

最佳答案

试试这个:

<script>
    var parcelRequire;
</script>

(来自 here 。)

关于angular - 如何删除在ionic中peerjs没有定义parcelRequire,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62956200/

相关文章:

javascript - Angular 6 : table with *ngIF generates one <td> more than i need

Cordova:本地文件系统未定义

angular - 警告 : Can't resolve all parameters for Storage in PATH/node_modules/@ionic/storage/es2015/storage. d.ts : (? )

javascript - 使用您知道将在 Javascript 和 Angular 中创建的对象的函数(以 Peerjs 和 Socket.io 为特色)

node.js - 使用 npm 安装 Peerjs 会导致警告

angular - 单击按钮并通过 component - angular 增加数字

angular - 在 Shippable 上出现 TypeScript 错误,但在本地却没有

angular - 等待多个可观察对象(并行)在 Angular 效果中完成

scroll - 垂直滚动不适用于 ionic 中的水平滚动列表

javascript - WebRTC 视频流无法通过 http 工作