我正在开发一个用 TypeScript 2 编写的 angular 2 项目,我目前正面临导入机制的问题。
我项目的每个子文件夹里面都有一个“index.ts”文件,即导出所述文件夹包含的类。
所以,在我的“app”目录中,我有
- app.component.ts
- app.module.ts
- 应用程序.routes.ts
然后,一个 index.ts 文件包含:
export * from './app.component';
export * from './app.module';
export * from './app.routes';
我的问题是我无法从位于同一目录中的文件中导入导出的类。
例如,在我的 app.module.ts 中,我想导入 app 组件。如果我做 :
import { AppComponent } from './app.component';
一切正常!在编译时和运行时没有错误。生活很酷,生活很美好。
但我不能执行以下操作:
import { AppComponent } from '.'; // or './', or even './index'
IDE (Visual Studio) 实际上正确地解析了导入(它编译时没有错误)。我什至从 Intellisence 获得自动完成功能......
但我在运行时收到此错误:
Error: Unexpected value 'undefined' imported by the module 'AppModule'
我只是不知道为什么。
注意:从子文件夹中的 index.ts 导入没有任何错误(例如,我可以从也有索引的 './core' 导入)。
先感谢您 :)