Jak poprawnie wyeksportować klasę ES6 w Node 4?
Definiuję klasę w module:
"use strict";
var AspectTypeModule = function() {};
module.exports = AspectTypeModule;
var AspectType = class AspectType {
// ...
};
module.export.AspectType = AspectType;
Ale dostaję następujący komunikat o błędzie:
TypeError: Cannot set property 'AspectType' of undefined
at Object.<anonymous> (...\AspectType.js:30:26)
at Module._compile (module.js:434:26)
....
Jak wyeksportować tę klasę i użyć jej w innym module? Widziałem inne pytania tak, ale dostaję inne komunikaty o błędach, gdy próbuję wdrożyć ich rozwiązania.
9 answers
Jeśli używasz ES6 w Node 4, nie możesz używać składni modułu ES6 bez transpilera, ale Moduły CommonJS (standardowe moduły węzła) działają tak samo.
module.export.AspectType
Powinno być
module.exports.AspectType
Stąd komunikat o błędzie "Cannot set property 'AspectType' of undefined " ponieważ module.export === undefined
.
Również dla
var AspectType = class AspectType {
// ...
};
Możesz po prostu napisać
class AspectType {
// ...
}
I uzyskać zasadniczo to samo zachowanie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-18 17:40:04
// person.js
'use strict';
module.exports = class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
display() {
console.log(this.firstName + " " + this.lastName);
}
}
// index.js
'use strict';
var Person = require('./person.js');
var someone = new Person("First name", "Last name");
someone.display();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-24 07:24:56
wyrażenie klasy może być używany dla prostoty.
// Foo.js
'use strict';
// export default class Foo {}
module.exports = class Foo {}
-
// main.js
'use strict';
const Foo = require('./Foo.js');
let Bar = new class extends Foo {
constructor() {
super();
this.name = 'bar';
}
}
console.log(Bar.name);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-07 09:13:18
Użyj
// aspect-type.js
class AspectType {
}
export default AspectType;
Następnie zaimportować
// some-other-file.js
import AspectType from './aspect-type';
Czytaj http://babeljs.io/docs/learn-es2015/#modules Po Więcej Szczegółów
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-18 17:21:04
W ECMAScript 2015 możesz eksportować i importować wiele klas, takich jak Ta
class Person
{
constructor()
{
this.type = "Person";
}
}
class Animal{
constructor()
{
this.type = "Animal";
}
}
module.exports = {
Person,
Animal
};
To gdzie ich używasz:
const { Animal, Person } = require("classes");
const animal = new Animal();
const person = new Person();
W przypadku kolizji nazw, lub wolisz inne nazwy, możesz je zmienić w następujący sposób:
const { Animal : OtherAnimal, Person : OtherPerson} = require("./classes");
const animal = new OtherAnimal();
const person = new OtherPerson();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-02 20:50:43
Kilka innych odpowiedzi jest zbliżonych, ale szczerze mówiąc, myślę, że lepiej będzie wybrać najczystszą, najprostszą składnię. Po zażądał sposobu eksportu klasy w ES6 / ES2015. Nie wydaje mi się, że można dostać dużo czystsze niż to:
'use strict';
export default class ClassName {
constructor () {
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-25 18:11:34
Po prostu piszę to w ten sposób
W pliku AspectType:
class AspectType {
//blah blah
}
module.exports = AspectType;
I zaimportować go w ten sposób:
const AspectType = require('./AspectType');
var aspectType = new AspectType;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-14 09:21:32
Miałem ten sam problem. Znalazłem to, że nazwałem mój obiekt o tej samej nazwie, co nazwa klasy. przykład:
const AspectType = new AspectType();
To wszystko spieprzyło...
mam nadzieję, że to pomożeWarning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-21 12:50:17
Czasami muszę zadeklarować kilka klas w jednym pliku lub chcę wyeksportować klasy bazowe i zachować ich nazwy eksportowane, ponieważ mój edytor JetBrains rozumie to lepiej. I just use
global.MyClass = class MyClass { ... };
I gdzie indziej:
require('baseclasses.js');
class MySubclass extends MyClass() { ... }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-15 14:09:55