Javascript Node.js NPM和Typescript可以';t从测试应用程序访问类函数
我正在编写一个NPM包,以便可以重用一个类。包安装良好,可以加载类,但是我无法访问类中的函数。我正在使用typescript,然后将其编译成JS类: 例如,编译输出Javascript Node.js NPM和Typescript可以';t从测试应用程序访问类函数,javascript,node.js,class,typescript,npm,Javascript,Node.js,Class,Typescript,Npm,我正在编写一个NPM包,以便可以重用一个类。包安装良好,可以加载类,但是我无法访问类中的函数。我正在使用typescript,然后将其编译成JS类: 例如,编译输出 --------------------------------- FILE ./node_modules/test/index.js --------------------------------- class Test{ constructor(){ } hello(name){
---------------------------------
FILE ./node_modules/test/index.js
---------------------------------
class Test{
constructor(){
}
hello(name){
console.log("Hello " + name);
}
}
module.exports.Test = Test; // Note I have also tried module.exports = Test
---------------
FILE: ./test.js
---------------
var test = require('test');
console.log(test) // ok, object is defined.
test.hello('THERE'); // error
---------------
# node test.js
// Error
test.hello('Hello');
^
TypeError: test.hello is not a function
at Object.<anonymous> (/Users/Tony/test/index.js:6:6)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:423:7)
at startup (bootstrap_node.js:1
-------------------------------------
文件./node_modules/test/index.js
---------------------------------
课堂测试{
构造函数(){
}
你好(姓名){
console.log(“Hello”+name);
}
}
module.exports.Test=测试;//注:我还尝试了module.exports=Test
---------------
文件:./test.js
---------------
var测试=要求(“测试”);
log(test)//确定,对象已定义。
test.hello('THERE');//错误
---------------
#node test.js
//错误
test.hello('hello');
^
TypeError:test.hello不是函数
反对。(/Users/Tony/test/index.js:6:6)
在模块处编译(模块js:571:32)
在Object.Module.\u extensions..js(Module.js:580:10)
在Module.load(Module.js:488:32)
在tryModuleLoad时(module.js:447:12)
在Function.Module.\u加载(Module.js:439:3)
位于Module.runMain(Module.js:605:10)
运行时(bootstrap_node.js:423:7)
启动时(bootstrap_node.js:1
我认为您需要创建一个类的实例来调用其上的方法,对吗?下面也是这样:
var Test = require('test');
var test = new Test();
test.hello('THERE');
按预期工作
如果没有,则
/node\u modules/test
中的package.json是否有指向index.js
的main
键?我认为您需要创建类的实例来调用其上的方法,对吗
var Test = require('test');
var test = new Test();
test.hello('THERE');
按预期工作
如果没有,则
/node\u modules/test
中的package.json是否有指向index.js
的main
键?有两种实现方法:
- 类和实例:如果要创建任何要扩展、创建新对象等的类对象,则必须从类
中创建新实例:Test
const Test = require('./test'); const test = new Test(); test.hello('Kai'); // 'Hello Kai';
- 静态函数:如果您只想创建一个拥有自己的
:函数的类
class Test { constructor(){ } static hello(name){ console.log("Hello " + name); } } module.exports = Test; //test.js const Test = require('./test') Test.hello('Kai'); // 'Hello Kai';
有两种方法可供您实施:
- 类和实例:如果要创建任何要扩展、创建新对象等的类对象,则必须从类
中创建新实例:Test
const Test = require('./test'); const test = new Test(); test.hello('Kai'); // 'Hello Kai';
- 静态函数:如果您只想创建一个拥有自己的
:函数的类
class Test { constructor(){ } static hello(name){ console.log("Hello " + name); } } module.exports = Test; //test.js const Test = require('./test') Test.hello('Kai'); // 'Hello Kai';
对我来说,这是编程的基础:类和实例。@kai,反应很好,对你有好处!对我来说,这是编程的基础:类和实例。@kai,反应很好,对你有好处!谢谢,是的,就是这样。谢谢,是的,就是这样。