简体   繁体   English

无法将TypeScript服务器端与node.js一起使用(仅继承)

[英]Unable to use TypeScript server side with node.js (Inheritance only)

I don't understand why inheritance in TS is so tricky. 我不明白为什么TS中的继承如此棘手。 It doesn't works on my 'Controllers'. 它不适用于我的“控制器”。 I tried to compile with both AMD and commonJS, same result. 我尝试使用AMD和commonJS进行编译,结果相同。

UserController 用户控制器

///<reference path='../../../../shared/lib/public/def/defLoader.d.ts'/>
///<reference path='Controller.ts'/>

import express = require("express");
import controller = require("./Controller");

export class UserController extends controller.Controller{

    public constructor(req: express.Request, res: express.Response){
        super();
    }

    public  __before(){
        console.log('before');
    }

    public  __repartitor(){
        console.log('repartitor');
    }

    public  test(){
        console.log('test');
    }

    public  default(){
        console.log('default');
    }

}

Controller (parent) 控制器 (父级)

///<reference path='../../../../shared/lib/public/def/defLoader.d.ts'/>
import express = require("express");

export class Controller{

    public static beforeSharedPath: string = path.join(__dirname, "../../public");
    public static publicPath: string = path.join(__dirname, '../../');
    public static sharedPath: string = Controller.beforeSharedPath+"/shared";
    public static webServerPath: string = path.join(__dirname, '../../');

    public req: express.Request;
    public res: express.Response;

    public constructor(req: express.Request, res: express.Response){
        this.req = req;
        this.res = res;
    }

    public static __before(req: express.Request, res: express.Response, next: Function){
        next();
    }

    public static __after(req: express.Request, res: express.Response, next: Function){
        next();
    }

}

In another script: 在另一个脚本中:

 var obj = require('./../app/controllers/' + controllerName+".js")

The previous code call: 先前的代码调用:

module.exports = require('./ts/UserController.ts').UserController;

But I got a obj undefined. 但是我有一个未定义的obj If I remove the extends and call() I got the object. 如果我删除了extends和call(),我就得到了对象。 I just don't understand, I tried to fix it 3 hours without get any good result. 我只是不明白,我试图将其修复3个小时,但未取得任何良好的结果。

There is some tutorial to use TS on nodejs server side? 有一些在nodejs服务器端使用TS的教程吗? ... I use TS somewhere else and it works for both server + client with the same code, but it's the same, when I extends I got troubles. ...我在其他地方使用TS,它适用于具有相同代码的服务器和客户端,但是都是一样的,当我扩展时遇到了麻烦。

Your UserController and Controller code is valid and it should work. 您的UserControllerController代码是有效的,应该可以使用。

Error seems to be in var obj = require('./../app/controllers/' + controllerName+".js") You should not need the js extension. 错误似乎出在var obj = require('./../app/controllers/' + controllerName+".js")您不需要js扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM