在React项目中,我找到了这段代码,这是非常标准的:
import React from 'react';
import {
ImageLeft,
ImageRight,
ImageBottom,
ImageBanner
} from './content';
const LAYOUT_COMPONENT = {
image_left: ImageLeft,
image_right: ImageRight,
image_bottom: ImageBottom,
image_banner: ImageBanner
};
LAYOUT_COMPONENT对象有重复。是否有可能以某种方式使用import语句中的destructuring创建它,以避免额外的代码?
答案 0 :(得分:1)
No, there is no destructuring in import statements.
What you could do is
import * as contents from './content';
const LAYOUT_COMPONENT = {};
for (let p of ["Left", "Right", "Bottom", "Banner"])
LAYOUT_COMPONENT["image_"+p.toLowerCase()] = contents["Image"+p];
}
but notice this will prevent a module bundler from tree shaking.