1
我创建的非常简单的模块用于测试此尝试的可行性。这里是SPServerApp.ts
开头:如何使用Typescript创建node.js模块
class SPServerApp {
public AllUsersDict: any;
public AllRoomsDict: any;
constructor() {
this.AllUsersDict = {};
this.AllRoomsDict = {};
}
}
module.exports = SPServerApp();
在我的应用程序
然后,我有这样的要求声明:
var serverapp = require('./SPServerApp');
然后我尝试访问其中的一个词典,像这样:
serverapp.AllUsersDict.hasOwnProperty(nickname)
但是得到错误:
TypeError: Cannot read property 'hasOwnProperty' of undefined
任何人都可以看到我在这里做错了吗?
谢谢,E.
您还没有实例化类。添加'新'或创建一个新的实例,你需要它。 – Phix
确实有效。谢谢Phix。 –
我认为这个链接会帮助你。 http://stackoverflow.com/questions/23739044/how-do-you-write-a-node-module-using-typescript –