gpt4 book ai didi

angular - 无法解析来自同一目录中脚本的 index.ts 的导入

转载 作者:太空狗 更新时间:2023-10-29 17:22:59 29 4
gpt4 key购买 nike

我正在做一个用 TypeScript 2 编写的 angular 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';

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

例如,在我的 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”导入)。

提前谢谢你:)

最佳答案

我正好有 same issue .

您似乎有循环依赖

当你这样写的时候:

import { AppComponent } from '.';

解析器转到 index.ts 并看到:

export * from './app.component';

然后它转到 ./app.component 并看到这个:

import { AppComponent } from '.';

所以它转到 index.ts 并看到这个:

export * from './app.component';

等等……

奇怪的是你没有收到任何警告,而且,根据加载程序,它实际上可能第二次正确解析(所以第一次你得到未定义但在随后的调用中它正确解析) - 我已经花了 4小时,因为不太明显的循环依赖。我强烈认为 typescript 应该对这些事情发出警告,因为它是一个适当的蠕虫 jar 头。

关于angular - 无法解析来自同一目录中脚本的 index.ts 的导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39962047/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com