2016-05-31 33 views
6

如何組織像Java程序包一樣的Angular 2應用程序文件夾結構?如何組織我的Angular應用程序文件夾(如Java包)?

考慮以下項目佈局:

app 
|_model 
|_component 
|_service 

我想從服務導入foo.service.tsbar.component.ts組件。但據我所知,Angular 2的導入只支持/../service/這樣的相對路徑,這似乎是非常笨重的解決方案。

有沒有一種方法可以從根文件夾引用絕對洗澡的文件夾,就像使用Java軟件包一樣?

回答

3

UPDATE 2016年6月1日

npm install [email protected]你已經可以使用這個功能

我假設你的樹看起來像這樣:

node_modules 
|[email protected] 
|_rxjs 
app 
|_model 
|_component 
|_service 
package.json 
tsconfig.json 

您應該添加在你的tsconfig.json以下行:

{ 
    "compilerOptions": { 
    "baseUrl": ".", 
    "paths": { 
     "*": [ 
     "app/*", 
     "node_modules/*" 
     ] 
    } 
    } 
} 

然後像使用正@角度你可以參考你的模塊/ rxjs模塊

import { MyService } from 'service/MyService'; 
import { Component } from '@angular/core'; 
import { Observable } from 'rxjs'; 

注:的WebPack還需要webpack.config.js中的以下行

resolve: { 
    modulesDirectories: [ 
     'node_modules', 
     'app' 
    ] 
} 
+0

正如您已經提到的,這是在去年9月份以來的討論中(https://github.com/Microsoft/TypeScript/issues/5039)。我很驚訝他們之前沒有考慮過這個問題。與「../../../」一起使用非常麻煩。 感謝您的回答。 [email protected]即將推出; Beta已於6天前發佈:) – MaximeBernard

相關問題