javascript - 在 JavaScript 中导入带有子对象和数组的对象
问题描述
我有一个主题对象,它有许多对象和数组作为值。即,像这样:
export const theme = {
colors: {...list of colors},
breakpoints: [...array of breakpoints],
...
}
现在,假设我想访问另一个文档中的颜色对象和断点数组。目前,我正在导入这样的主题:
import { theme } from '../../theme'
const { colors, breakpoints: bp } = theme
}
我正在使用breakpoints: bp
这样我就可以breakpoints
在bp
我的文件中使用别名。
我想知道的是是否可以在 import 语句中完成所有这些操作。即,不是导入整个theme
对象,而是只导入颜色对象和断点数组。像这样的东西(这个代码不起作用,它只是为了说明这个想法):
import { theme.colors as colors, theme.breakpoints as bp } from '../../theme'
这可能吗?如果是这样,怎么做?
谢谢。
解决方案
您可能应该更改导出主题的方式。您可以只导出主题的每个属性 - 它就像删除 const 声明并仅导出对象一样简单。:
export {
colors: {...list of colors},
breakpoints: [...array of breakpoints],
...
}
然后像这样使用导入:
import { colors, breakpoints as bp } from '../../themes'
推荐阅读
- php - 使用 curl PHP 获取 API
- android - 等待片段被附加
- ios - 管道中的 Azure DevOps Fastlane 实现
- python - 一次对多列进行一次热编码并附加到主数据集?
- javascript - JavaScript单页应用,表单提交后如何显示正确的HTML?
- discord.py - 这个禁令命令不起作用......我该如何解决它?
- r - 如何在图例中添加垂直斜线或在 R 的线图中写一些东西?
- javascript - 使用 JS 正则表达式获取没有特定后缀的字符串段
- visual-studio - 如何从 TFS Checkin 隐藏敏感信息
- discord - 反应中的角色