【发布时间】:2015-09-23 23:52:57
【问题描述】:
我正在使用 Electron 创建一个小型桌面应用程序并使用 module.exports 导出。在“服务器”端,这工作正常。但是,当我在前端使用 module.exports 时,根据 Electron 文档,我得到了这个错误。
Uncaught TypeError: this.showProgressbar is not a function"
var ViewController = {
getPageCount: function (res) {
this.total = res;
this.showProgressbar(res);
},
showProgressBar: function (num) {
$('.progress-container').addClass('show');
$('.progress-bar').style('width', '0%');
}
};
module.exports = ViewController;
在客户端,这就是我访问此文件的方式。
var view = require(__dirname + '/client/ViewController.js');
ipc.on('page_count', view.getPageCount);
在这种情况下我应该如何访问内部方法?
【问题讨论】:
-
this.showProgressBar 正在查找 getPageCount 上的 showProgressbar,而不是在 ViewController 对象中。
-
如果你用这个呢?
ipc.on('page_count', view.getPageCount.bind(view)) -
你也需要用正确的大小写来调用你的函数:
this.showProgressBar(res);。
标签: javascript node.js electron