2016-12-30 119 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語句時,語法正在工作。 –