问题:我似乎无法实例化我从其他文件导入的类型脚本类的新实例。
说明:我在使用快速模块的类型脚本中编写节点应用程序。这些类在外部文件中,以封装express的路由。
文件' masterRoute.ts'是将任何传入的http请求转发到其预期的route.ts文件。
文件' indexRoute.ts'是网址的预期路由文件:' whatever.com /'。
目标:出于学习目的,我正在尝试在MasterRoute类中创建外部IndexRoute类的实例。
示例:
indexRoute.ts
import express = require('express');
export class IndexRoute{
private _ejsFilePath: string = 'views/index.ejs';
private request: express.Request;
private response: express.Response;
constructor(request: express.Request, response: express.Response){
this.request = request;
this.response = response;
}
public renderFile(): string{
try{
this.response.render(this._ejsFilePath);
return null;
}
catch(error){
return error;
}
}
}
masterRoute.ts
import express = require('express');
import * as IndexRoute from './indexRoute';
export class MasterRoute{
private Router: express.Router;
constructor(){
this.Router = new express.Router();
}
public onHttpRequest(request: express.Request, response: express.Response){
// I have tried to use all of the lines below one by one.
// None of them work.
// let test: IndexRoute = new IndexRoute(request, response);
// let test: = new IndexRoute(request, response);
// let test = IndexRoute(request, response);
this.Router.get('/');
}
}