问题是从home.ts生成的js找不到我的index.js类。我在Typescript中没有任何错误,但是在运行javascript时却遇到了一个错误。
TypeError:index_1.default.login不是函数 在/Users/Jannik/Documents/Web/Willhub-ts/dist/controllers/home.js:12:37 在对象。 (/用户/ Jannik /文档/Web/Willhub-ts/dist/controllers/home.js:15:3) 在Module._compile(internal / modules / cjs / loader.js:956:30) 在Object.Module._extensions..js(内部/模块/cjs/loader.js:973:10) 在Module.load(internal / modules / cjs / loader.js:812:32) 在Function.Module._load(内部/模块/cjs/loader.js:724:14) 在Module.require(internal / modules / cjs / loader.js:849:19) 在要求时(内部/模块/cjs/helpers.js:74:18) 在对象。 (/用户/ Jannik /文档/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());
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;
}
}
Index.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
答案 0 :(得分:1)
首先,您的constructLogin()
可能是错误的,因为我猜应该分配Index._login
:
private constructLogin(): void {
// -----\/------------------
Index._index = function (req: Request, res: Response, next) {
res.render("main", { "header-enabled": true, "nav-enabled": true })
const isLoggedIn: boolean = true;
}
}
如果已解决问题,请先创建实例再检查是否使用Index.login()
。那是因为:
Index.login
是一个返回Index._login
的getter函数Index.login()
调用结果Index._login
,但是... Index._login
中设置了constructLogin()
,并且... constructLogin()
函数中调用constructor()
如果在创建实例之前调用Index.login()
,则Index._login
将是undefined
:
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() {
// NOTICE: Assigns `_login` instead
Index._login = 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;
}
}
console.log(Index._login); // undefined
let i = new Index();
console.log(Index._login); // function