javascript - JS依赖优先级

标签 javascript typescript react-native

我的目录中有 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/

相关文章:

authentication - 是否可以自定义 aws-amplify-react-native 包的默认注册、登录屏幕?

javascript - 虚拟 DOM 的比较算法是否能够找出唯一的差异并修补真实 DOM?

javascript - 向下滚动时隐藏导航不适用于 barba.js 页面转换

typescript - 如何在 Typescript 中将接口(interface)转换为映射类型

objective-c - 如何从 React-Native 访问 rootViewController

javascript - React Native Scrollview 停止动力? (世博会)

javascript - 使用node.js创建大文件

javascript - 如何从私有(private)函数访问组件范围?

typescript - 无法使用 typescript 导入默认值

javascript - typescript 通过将具有相同键的对象的值组合来合并 2 个对象数组