简体   繁体   English

Es6解构

[英]Es6 Destructuring

I have a file that is similar to this: 我有一个与此类似的文件:

const COLORS = {
  PRIMARY_COLOR: 'red',
  SECONDARY_COLOR: 'green'
};

const APP = {
  APP_COLOR: GRAY_DARK,
  APP_FONT_SIZE: FONT_SIZE_NORMAL,
  APP_FONT_WEIGHT: FONT_WEIGHT_NORMAL,
  APP_SEPARATOR_COLOR: GRAY_LIGHT
};

export default {
  ...COLORS,
  ...APP
};

The issue is when I'm trying to destructure that object from another file, I get undefined values? 问题是,当我尝试从另一个文件中解构该对象时,得到未定义的值吗?

import theme, { PRIMARY_COLOR } from '../../../themes/default';

printing the theme object works fine but printing PRIMARY_COLOR gets undefined 打印主题对象工作正常,但打印PRIMARY_COLOR不确定

Any help? 有什么帮助吗?

The {} syntax in imports is for "named" imports and is not destructuring. 导入中的{}语法适用于“命名”导入,并且不构成破坏。

Just do this: 只要这样做:

import theme from '../../../themes/default';

const { PRIMARY_COLOR } = theme;

To understand the difference, you first need to know the way they are exported. 要了解差异,您首先需要知道它们的导出方式。

In case of React , the export goes something like this React情况下,导出是这样的

const Component = ...
...
...
export Component;

This becomes available under React.Component and you can import it like import { Component } from 'react'; 这在React.ComponentReact.Component ,您可以像import { Component } from 'react';一样import { Component } from 'react';import { Component } from 'react';

The way these look under the microscope is: 这些在显微镜下的外观是:

default.Component
...

While everything else is just under the default object. 而其他所有内容都位于default对象下。

If you do a quick console.log of theme , you'll understand what I mean. 如果您快速执行theme console.log ,您将理解我的意思。

I hope this makes sense. 我希望这是有道理的。


Let's go a little in depth. 让我们深入一点。

Suppose you have the following bit of code: 假设您具有以下代码:

const a = {
    test: 'hello',
};

const b = {
    foo: 'bar',
}

export default a;

Now, let's import that 现在,让我们import

import * as theme from './test.js'

When we do a console.log( theme ) we get 当我们做一个console.log( theme )我们得到

{ default: { test: 'hello' } }

What does this show? 这说明什么? It means that the export object of a file contains a default property which is automatically loaded into memory when we do something like import theme from 'test' . 这意味着文件的export对象包含一个default属性,当我们执行诸如import theme from 'test'类的操作时,该default属性会自动加载到内存中。 However, if you have more than one export , the compiler gives you the option to pick and choose, but at the same time, provides you with a default object just for fall back. 但是,如果您具有多个export ,则编译器会为您提供选择的选项,但同时,会为您提供一个default对象以供回退。

In your case, you have exported everything under the default . 在您的情况下,您已导出了default下的所有内容。 When you do import theme from './theme' all works fine. 当您import theme from './theme'一切正常。 However, when you do { PRIMARY_COLOR }... it tries to find something which was exported like 但是,当您执行{ PRIMARY_COLOR }...它将尝试查找已导出的内容,例如

export PRIMARY_COLOR...

I hope this makes it clear! 我希望这一点很清楚! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM