2016-12-30 123 views
0

使用进口时,这是文件级app.ts无法在打字稿

import Express = require('express'); 
    import FileSystem = require('fs'); 
    import Http = require('http'); 

module Service { 
    export interface Configuration { 
     Port: number, 
     Host: string 
    } 
    export class AppServer { 
     App: Express.Application; 
     AppServer: Http.Server; 

     constructor() { 
      this.App = Express(); 
      this.App.use(this.App.route); 
      // this.App.use(this.App.router); 

     } 

     /** 
     * Start the server 
     * @param {Configuration} config 
     */ 
     StartServer = function (config: Configuration) { 
      var That = this; 
      this.AppServer = this.App.listen(config.Port, function() { 

       var Host = That.AppServer.address().address; 
       var Port = That.AppServer.address().port; 

       console.log("Example app listening at http://%s:%s", Host, Port) 

      }) 
     } 
    } 
} 

当我访问该命名空间在另一个文件中的代码来访问命名空间或类,我得到的编译错误“无法寻找服务“。

这是文件中的代码-server.ts

/// <reference path="App.ts" /> 

var Server = new Service.AppServer(); 
var Config = <Service.Configuration>{ 
    Port: 3000 
} 
Server.StartServer(Config); 

但是当我删除它需要HTTP和表达import语句,我也不多收到错误的文件中。 请帮我找到 - 我在哪里做错了?

错误我是gettinf是 - 'ts2304'找不到名字'service'。

+1

你能引用错误信息吗? – enkryptor

+0

根据错误你无法找到哪种服务?它是用于你的'Service'模块还是用于'express','fs'或'http'? – ranakrunal9

+0

我已经更新了问题,顺便说一句,我得到的错误是'ts2304'找不到名称'服务'。 –

回答

2

看来你还没有导出模块。尝试在文件末尾添加export {Service}

+0

我已经尝试了您的建议,它不工作。 –

+1

导出后,在server.ts中导入了'Service'。像这样:从'./app.ts'导入{Service}。 ' –

+0

我正在尝试像这样导入 - import Service = Service;但我收到错误 - 无法找到服务。但是当我像你一样做的时候,我不会再犯这个错误了。 你能告诉我,为什么我的语法不起作用。但是当我移除require语句时,语法正在工作。 –