关于javascript:如何组织Java包等Angular应用文件夹?

How can I organize my Angular app folders like a Java package?

如何组织Angular 2应用程序文件夹结构(如Java包)?

考虑以下项目布局:

1
2
3
4
app
 |_model
 |_component
 |_service

我想将foo.service.ts从服务导入到组件中的bar.component.ts。 但是据我所知,Angular 2导入仅支持相对路径,例如/../service/,这似乎很笨拙。

有没有办法像Java包一样从根文件夹引用具有绝对浴池的文件夹?


更新2016-06-01

使用npm install typescript@next,您已经可以使用此功能

我假设你的树看起来像这样:

1
2
3
4
5
6
7
8
9
node_modules
 |_@angular
 |_rxjs
app
 |_model
 |_component
 |_service
package.json
tsconfig.json

您应该在tsconfig.json中添加以下几行:

1
2
3
4
5
6
7
8
9
10
11
{
 "compilerOptions": {
   "baseUrl":".",
   "paths": {
     "*": [
       "app/*",
       "node_modules/*"
      ]
    }
  }
}

那么您可以像使用常规@ angular / rxjs模块一样引用您的模块

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

注意:webpack在webpack.config.js中还需要以下几行

1
2
3
4
5
6
resolve: {
    modulesDirectories: [
        'node_modules',
        'app'
    ]
}


尚未,TypeScript 2.0应该很快会出现

看这里
https://github.com/Microsoft/TypeScript/issues/5039