打字稿编译的Javascript代码不起作用(variable.default.function())

时间:2019-10-18 05:25:29

标签: javascript typescript

问题是从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

1 个答案:

答案 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