javascript - ES6 导入/导出未按预期运行

标签 javascript reactjs react-native babeljs es6-modules

我不确定它是否与 react-native 相关,但这些是我的版本:

“ native react ”:“0.46.4”, "babel-preset-react-native": "2.1.0",

// src/utils/a.js
export default 'a file works!'

// src/utils/b.js
export default 'b file works!'

// src/utils/index.js
import a from './a'
import b from './b'

export { a, b }

基本上当我尝试:

import * as utils from 'src/utils'
// or
import { a, b } from 'src/utils'

它返回未定义的“a”和“b”属性,例如

utils = { a: undefined, b: undefined }

我不知道我在这里做错了什么,我的猜测是那些 a/b.js 文件没有在它们应该加载的时候加载,我之前做的一个 hack 是在监听函数上,utils.auth,我必须在其中 if (auth && auth.listener),这很有效,因为当应用程序启动时,监听器是未定义的,但随后是正确的在它成为它应该成为的样子之后。

编辑: 似乎如果我尝试:

// src/utils/index.js
const a = 'a works'
const b = 'b works'

export { a, b }

结果是一样的

最佳答案

您基本上可以导出文件而无需导入它。

查看我的组件文件夹中的 index.js:

export {Content} from './Content'
export {FilterSelect} from './FilterSelect'
export {ListItem} from './ListItem'
export {Searchbar} from './Searchbar'
export {Sidebar} from './Sidebar'
export {RequiredArgument} from './RequiredArgument'
export {Loading} from './Loading'
export {ArgumentProvided} from './ArgumentProvided'
export {OverviewBar} from './OverviewBar'
export {Home} from './Home'
export {Layout} from './Layout'

这就是我导入它们的方式

import { Content, FilterSelect, ListItem, Searchbar, Sidebar, Loading, RequiredArgument, ArgumentProvided, OverviewBar} from './components/index.js'
import Layout from './components/Layout''

关于javascript - ES6 导入/导出未按预期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45840030/

相关文章:

javascript - 如何禁用 JWPlayer 中的高清按钮?

javascript - Onclick on td 不起作用

reactjs - 电子商务跟踪数据未通过 react-ga 发送

javascript - 在react js应用程序上动态地将参数传递给Web服务

css - 如何在 img 组件上制作 prop !重要?

javascript - react native : How to switch page without using NavigatorIOS component?

javascript - 不变违规 : View config not found for name div. 确保组件名称以大写字母开头

javascript - 加载页面预加载器时隐藏滚动条

javascript - AWS 从客户端上传文件到 S3

node.js - 是否可以在 Node.js 后端使用 SignalR?