无法从位于同一目录中的脚本解析index.ts中的导入

kip*_*ipy 6 import typescript dependency-resolver angular

我正在研究用TypeScript 2编写的角度2项目,我目前面临着导入机制的问题.

我项目的每个子文件夹里面都有一个"index.ts"文件,即导出所述文件夹包含的类.

所以,在我的"app"目录中,我有

  • app.component.ts
  • app.module.ts
  • app.routes.ts

然后,index.ts文件包含:

export * from './app.component';
export * from './app.module';
export * from './app.routes';
Run Code Online (Sandbox Code Playgroud)

我的问题是我无法从位于同一目录中的文件导入导出的类.

例如,在我的app.module.ts中,我想导入应用程序组件.如果我做 :

import { AppComponent } from './app.component';
Run Code Online (Sandbox Code Playgroud)

一切正常!编译时和运行时没有错误.生活很酷,生活很美好.

但我不能做到以下几点:

import { AppComponent } from '.'; // or './', or even './index'
Run Code Online (Sandbox Code Playgroud)

IDE(Visual Studio)实际上正确解析了导入(它编译时没有错误).我甚至从Intellisence获得自动完成功能......

但是我在运行时遇到这个错误:

 Error: Unexpected value 'undefined' imported by the module 'AppModule'
Run Code Online (Sandbox Code Playgroud)

我只是不知道为什么.

注意:通过从子文件夹中的index.ts导入我没有任何错误(例如,我可以从也有索引的'./core'导入).

先感谢您 :)

Izh*_*aki 11

我有完全相同的问题.

你似乎有循环依赖.

当你写这个:

import { AppComponent } from '.';
Run Code Online (Sandbox Code Playgroud)

解析器去index.ts看看:

export * from './app.component';
Run Code Online (Sandbox Code Playgroud)

那么它去./app.component看看这个:

import { AppComponent } from '.';
Run Code Online (Sandbox Code Playgroud)

所以它去index.ts看看:

export * from './app.component';
Run Code Online (Sandbox Code Playgroud)

等等等等...

有什么古怪,这是你不会看到任何警告,并根据装载机,它实际上可能正确地围绕解决第二次(这样你第一次得到了一个未定义但在后续调用它正确解析) - 我已经花了,因为4小时一个远不那么明显的循环依赖.我强烈反对打字稿应该对这些事情发出警告,因为它是一种适当的蠕虫病毒.