如何在 ES6 模块中导入部分对象

How to import part of object in ES6 modules

react documentation中我找到了这种导入PureRenderMixin的方法

var PureRenderMixin = require('react/addons').addons.PureRenderMixin;

怎么改成ES6风格。我唯一能做的就是:

import addons from "react/addons";
let PureRenderMixin = addons.addons.PureRenderMixin;

希望有更好的办法

不幸的是import statements does not work like object destructuring。这里的大括号表示要导入这个名称的令牌,而不是默认导出的属性。看看这对 import/export:

 //module.js
 export default 'A';
 export var B = 'B';

 //script.js
 import A from './a.js';  //import value on default export
 import {B} from './a.js'; // import value by its name
 console.log(A, B); // 'A', 'B'

对于您的情况,您可以导入整个对象并进行解构赋值

 import addons from "react/addons";
 let {addons: {PureRenderMixin}} = addons;
import PureRenderMixin from 'react-addons-pure-render-mixin';

参见示例 here