Jér*_*nge 96 javascript module export node.js
我在模块中定义了一个类:
"use strict";
var AspectTypeModule = function() {};
module.exports = AspectTypeModule;
var AspectType = class AspectType {
// ...
};
module.export.AspectType = AspectType;
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误消息:
TypeError: Cannot set property 'AspectType' of undefined
at Object.<anonymous> (...\AspectType.js:30:26)
at Module._compile (module.js:434:26)
....
Run Code Online (Sandbox Code Playgroud)
我该如何导出这个类并在另一个模块中使用它?我已经看到了其他SO问题,但是当我尝试实现他们的解决方案时,我收到了其他错误消息.
log*_*yth 104
如果您在节点4中使用ES6,则不能在没有转换器的情况下使用ES6模块语法,但CommonJS模块(Node的标准模块)的工作方式相同.
module.export.AspectType
Run Code Online (Sandbox Code Playgroud)
应该
module.exports.AspectType
Run Code Online (Sandbox Code Playgroud)
因此错误消息"无法设置属性'AspectType'未定义"因为module.export === undefined.
另外,为
var AspectType = class AspectType {
// ...
};
Run Code Online (Sandbox Code Playgroud)
你能写一下吗?
class AspectType {
// ...
}
Run Code Online (Sandbox Code Playgroud)
并获得基本相同的行为.
小智 99
// person.js
'use strict';
module.exports = class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
display() {
console.log(this.firstName + " " + this.lastName);
}
}
Run Code Online (Sandbox Code Playgroud)
// index.js
'use strict';
var Person = require('./person.js');
var someone = new Person("First name", "Last name");
someone.display();
Run Code Online (Sandbox Code Playgroud)
Jon*_*del 36
使用ECMAScript 2015,您可以导出和导入多个类
class Person
{
constructor()
{
this.type = "Person";
}
}
class Animal{
constructor()
{
this.type = "Animal";
}
}
module.exports = {
Person,
Animal
};
Run Code Online (Sandbox Code Playgroud)
然后你在哪里使用它们:
const { Animal, Person } = require("classes");
const animal = new Animal();
const person = new Person();
Run Code Online (Sandbox Code Playgroud)
如果出现名称冲突,或者您更喜欢其他名称,可以像下面这样重命名:
const { Animal : OtherAnimal, Person : OtherPerson} = require("./classes");
const animal = new OtherAnimal();
const person = new OtherPerson();
Run Code Online (Sandbox Code Playgroud)
Tha*_*you 14
使用
// aspect-type.js
class AspectType {
}
export default AspectType;
Run Code Online (Sandbox Code Playgroud)
然后导入它
// some-other-file.js
import AspectType from './aspect-type';
Run Code Online (Sandbox Code Playgroud)
阅读http://babeljs.io/docs/learn-es2015/#modules了解更多详情
mas*_*tic 12
类表达式可以用于简单.
// Foo.js
'use strict';
// export default class Foo {}
module.exports = class Foo {}
Run Code Online (Sandbox Code Playgroud)
-
// main.js
'use strict';
const Foo = require('./Foo.js');
let Bar = new class extends Foo {
constructor() {
super();
this.name = 'bar';
}
}
console.log(Bar.name);
Run Code Online (Sandbox Code Playgroud)
小智 11
我只是这样写
在AspectType文件中:
class AspectType {
//blah blah
}
module.exports = AspectType;
Run Code Online (Sandbox Code Playgroud)
并像这样导入:
const AspectType = require('./AspectType');
var aspectType = new AspectType;
Run Code Online (Sandbox Code Playgroud)
其他几个答案很接近,但老实说,我认为你最好采用最干净,最简单的语法.OP要求在ES6/ES2015中输出一个类.我认为你不能比这更清洁:
'use strict';
export default class ClassName {
constructor () {
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
187217 次 |
| 最近记录: |