【发布时间】:2020-02-15 01:44:22
【问题描述】:
问题是 home.ts 生成的 js 没有找到我的 index.js 类。我在 Typescript 中没有错误,但我在运行 javascript 时遇到了错误。
TypeError: index_1.default.login 不是函数 在 /Users/Jannik/Documents/Web/Willhub-ts/dist/controllers/home.js:12:37 在对象。 (/Users/Jannik/Documents/Web/Willhub-ts/dist/controllers/home.js:15:3) 在 Module._compile (internal/modules/cjs/loader.js:956:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10) 在 Module.load (internal/modules/cjs/loader.js:812:32) 在 Function.Module._load (internal/modules/cjs/loader.js:724:14) 在 Module.require (internal/modules/cjs/loader.js:849:19) 在需要(内部/模块/cjs/helpers.js:74:18) 在对象。 (/Users/Jannik/Documents/Web/Willhub-ts/dist/app.js:20:24) 在 Module._compile (internal/modules/cjs/loader.js:956:30)
知道这可能来自哪里吗?
Home.ts:
router.get('/', Index.login());
router.get('/', Index.index());
Home.js:
router.get('/', index_1.default.login());
router.get('/', index_1.default.index());
索引.ts
import {Request, Response} from 'express';
export default class Index {
private static _index: Function;
private static _login: Function;
constructor(){
this.constructIndex();
this.constructLogin();
}
//Private Methods:
private constructIndex(): void {
Index._index = function (req: Request, res: Response, next) {
res.render("main", { "header-enabled": true, "nav-enabled": true })
next();
}
}
private constructLogin(): void {
Index._index = function (req: Request, res: Response, next) {
res.render("main", { "header-enabled": true, "nav-enabled": true })
const isLoggedIn: boolean = true;
}
}
//Public Methods:
public static get index() : Function {
return this._index;
}
public static get login(): Function {
return this._login;
}
}
索引.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Index {
constructor() {
this.constructIndex();
this.constructLogin();
}
//Private Methods:
constructIndex() {
Index._index = function (req, res, next) {
res.render("main", { "header-enabled": true, "nav-enabled": true });
next();
};
}
constructLogin() {
Index._index = function (req, res, next) {
res.render("main", { "header-enabled": true, "nav-enabled": true });
const isLoggedIn = true;
};
}
//Public Methods:
static get index() {
return this._index;
}
static get login() {
return this._login;
}
}
exports.default = Index;
//# sourceMappingURL=index.js.map
【问题讨论】:
标签: javascript typescript