我正在尝试创建可以在MacOS上管理Airplay的Electron应用程序

我正在使用Angular和TypeScript来包装来自此npm包Airplay npm package的API:

这是我在TypeScript中使用的代码:

export class AirplaySharing {
    public init() {
        const airplayer = window['require']('airplayer');
        const list = airplayer();

        list.on('update', function(player) {
            console.log('Found new AirPlay device:', player.name);
        });
    }
}

我在调用init()时收到此错误

最佳答案

这项工作会吗

import airplayer = require('airplayer');

export class AirplaySharing {
    public init() {
        const list = airplayer();
        list.on('update', function(player) {
            console.log('Found new AirPlay device:', player.name);
        });
    }
}

使用typescript构造函数也更好吗?像这样:
import airplayer = require('airplayer');

export class AirplaySharing {
    constructor(public list: airplayer()) {
        list.on('update', function(player) {
            console.log('Found new AirPlay device:', player.name);
        });
    }
}

希望能对您有所帮助:)

10-06 11:36