如何正确连接控制器与接口?

时间:2016-10-04 11:56:43

标签: node.js express typescript

我无法实现接口的方法,我得到错误ReferenceError: INew is not defined而不是

我有以下文件:

  • /services/newService.ts
  • /controllers/NewController.ts
  • /interfaces/INew.ts
  • /repositories/NewRepository.ts

1个新服务

import express         = require("express");
import NewController   = require("../controllers/NewController");
var router = express.Router();

class NewRoutes {
  private _newController: NewController;

  constructor () {
    this._newController = new NewController();
  }

  get routes () {
    var controller = this._newController;
    router.get("/new", controller.retrieve);
    return router;
  }

}

Object.seal(NewRoutes);
export = NewRoutes;

2控制器

import INew = require("../interfaces/INew");
class NewController{
  private _INew: INew;

  constructor(){
    this._INew = new INew(); // !!!! INew became RED --> cannot find name 'INew'
  }

  retrieve(req: express.Request, res: express.Response): void {
    try {
      console.log('--------------NewController-----------------');
      this._INew.findById(2);
    }
    catch (e)  {
      console.log(e);
    }
  }

}

Object.seal(NewController);
export = NewController;

3 INew

interface INew {
  findById: (id: number) => void;
}
export = INew;

4 NewRepository

import INew = require("../interfaces/INew");
class NewRepository implements INew {
    constructor() {
    }
    
    findById(id) {
        console.log('RESULT');
    }
}

Object.seal(NewRepository);
export = NewRepository;

当我尝试构建应用时,我收到错误:

 this._INew = new INew();
                  ^
ReferenceError: INew is not defined

请帮助

0 个答案:

没有答案