如何使用requireJs加载commonJS模块(QWebChannel,AngularJS)

时间:2018-06-14 12:46:09

标签: javascript angularjs requirejs commonjs r.js

目前正在研究一个我根本不掌握的主题(初级前端开发)。我希望使用qwebchannels来通过AngularJS服务在我的应用程序和c ++服务器之间接收和传递数据。我必须使用qwebchannel.js lib才能这样做。

问题是我不能使用这个lib而不包装它以使它可以与requireJS一起使用但是我迷路了。花了一些时间阅读文档和类似的问题,而无法正确加载和使用我的库,甚至真的所有这些......

这就是我所拥有的:

qwebchannel.js

var QWebChannel = function(transport, initCallback) {
    // Doing some things
}

module.exports = { QWebChannel: QWebChannel }

require.config.ts

require.config({
    ...
    paths: {
        ...
        'qwebchannel' : 'path/to/my/file'
        ...
    },
    shim: {
        ...
        'qwebchannel': { exports: 'QWebChannel' }
        ...
    }
...

MyService.ts

// What I would like to do
import * as QWebChannel from 'qwebchannel'
...
// Then i'll use it suiting my needs

我尝试使用r.js将qwebchannel.js转换为可用于requireJs的东西,但它只是用这个包装代码:

converted_qwebchannel.js

define(function (require, exports, module) {
    var QWebChannel = function(transport, initCallback) {
        // Doing some things
    }

    module.exports = { QWebChannel: QWebChannel }
}

编译时遇到的错误是“无法找到模块'qwebchannel'”

当然做完全错误的事情,真的很感激一些帮助。

0 个答案:

没有答案