我的目录中有 3 个文件,如下所示:
models
|_ Service.js
|_ Agent.js
|_ index.js
Index.js
export * from Service;
export * from Agent;
Agent.js
class Agent {
id = 0
}
export {Agent}
Service.js
import {Agent} from '.'
class Service {
agent = new Agent();
}
export {Service}
但是当我想创建 Service
的实例时,Agent
类型未定义,而如果我更改 index.js
中导出的优先级> 问题就解决了。
index.js(更改优先级后)
export * from Agent;
export * from Service;
有人可以告诉我哪里出错了吗?
最佳答案
上面的代码包含此处不需要的循环依赖。
导入应该明确声明一个模块依赖于另一个模块。
应该是:
import {Agent} from './Agent'
class Service {
agent = new Agent();
}
export {Service}
在这种情况下,它们在index.js中导入的顺序并不重要。
关于javascript - JS依赖优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49221958/