【发布时间】:2019-01-31 17:17:05
【问题描述】:
我有一个这样的文件夹结构
.
├── models
│ └── System.js
└── src
├── app.js
├── index.js
└── routes
├── lemon
│ ├── auth
│ │ └── index.js
│ └── index.js
└── index.js
.
/models/System.js
class System {
constructor() {
this.test = null;
}
}
module.exports = System
.
/src/app.js
const express = require("express");
const _System = require("../models/System");
const app = express();
var System = new _System();
System.test = 1;
//..... other codes
.
/src/routes/lemon/auth/index.js
const express = require("express");
const _System = require("../../../../models/System");
const router = express.Router();
console.log(_System.test); //returns null
router.get('/', (req, res) => {
res.send("Hello World");
});
module.exports = router;
.
我的文件夹结构是这样的,我正在尝试将 /app.js 中定义的 System.test = 1 值共享给 /routes/柠檬/auth/index.js。 但我做不到,它总是返回 null。
有没有办法与子路由共享同一个类初始化?
PS:我知道我的代码不正确,我已经搜索了很多。暂时看不懂英文资源太多了,但是我真的去搜了。
【问题讨论】:
标签: node.js class inheritance subclass