javascript - 在 JavaScript 中导入带有子对象和数组的对象

标签 javascript ecmascript-6 javascript-import

我有一个主题对象,它有许多对象和数组作为值。即,像这样:

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'

关于javascript - 在 JavaScript 中导入带有子对象和数组的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58838609/

相关文章:

javascript - 如何设置gmail账户验证超时?

javascript - es6 未捕获引用错误 : class is not defined

angular - 如何在Angular 6中选择段落?

javascript - ES6 import 相当于 require() 没有导出

javascript - 闭包编译器无法编译保留字 "default"

javascript - 正则表达式在字符串中搜索日期

javascript - Canvas 未附加到列表中

javascript - 遍历对象 es6

javascript - 如何修复 'import declarations may only be on top level` ECMAScript 导入错误

javascript - 在 jquery 中使用多个 id