javascript - Typescript 编译的 Javascript 代码不起作用(variable.default.function())
问题描述
问题是从 home.ts 生成的 js 没有找到我的 index.js 类。我在 Typescript 中没有错误,但在运行 javascript 时出现了错误。
TypeError:index_1.default.login 不是 /Users/Jannik/Documents/Web/Willhub-ts/dist/controllers/home.js:12:37 在 Object 的函数。(/Users/Jannik/Documents/Web/Willhub-ts/dist/controllers/home.js:15:3) 在 Object.Module 的 Module._compile (internal/modules/cjs/loader.js:956:30)。 _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) 在 require (internal/modules/cjs/helpers.js:74:18) 在 Object. (/Users/Jannik/Documents/Web/Willhub-ts/dist/app.js:20:24) 在 Module._compile (internal/modules/cjs/loader.js:956:30)
知道这可能来自哪里吗?
主页.ts:
router.get('/', Index.login());
router.get('/', Index.index());
主页.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
解决方案
首先,您可能是错误的,因为我猜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
是一个 getter 函数,它返回Index._login
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
推荐阅读
- c++ - 指针类型的特殊重载
- java - 如何检查所有复选框?试图检查所有复选框,测试通过,但视觉上我没有看到它们被选中
- java - liquibase.update 不执行带有 runAlways 的 Liquibase changeSet
- sql - SQL 有条件地连接表
- python - 来自 NLTK、Gensim、Scikit Learn 的 API 调用
- visual-studio - 无法安装或卸载 Visual Studio 2017
- crystal-reports - Crystal Reports - 将月份和年份参数转换为命令 WHERE 子句的开始和结束日期
- sql - 如果不包括 group by 中的列名,则无法按列排序
- python - 在 Pygame 中将开始屏幕切换到游戏
- pandas - Spark中RDD和Dataframe有什么区别